C#学习教程:如何在Asp.Net中发送带附件的电子邮件分享


如何在Asp.Net中发送带附件的电子邮件

我需要在asp.net中附上我的电子邮件图片,该文件已添加到解决方案资源管理器中,但我不知道如何添加此电子邮件,请指导我

我目前的代码如下

public void SendMail() { try { string receiverEmailId = "name@exmp.com"; string senderName = ConfigurationManager.AppSettings["From"].ToString(); string mailServer = ConfigurationManager.AppSettings["SMTPServer"].ToString(); ; string senderEmailId = ConfigurationManager.AppSettings["SMTPUserName"].ToString(); string password = ConfigurationManager.AppSettings["SMTPPasssword"].ToString(); var fromAddress = new MailAddress(senderEmailId, senderName); var toAddress = new MailAddress(receiverEmailId, "Alen"); string subject = "subject"; string body = "body."; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(fromAddress.Address, password) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); } } catch (Exception ex) { } } 

您是否查看了MailMessage.Attachments属性(请参阅MSDN)?

 // create attachment and set media Type // see http://msdn.microsoft.com/de-de/library/system.net.mime.mediatypenames.application.aspx Attachment data = new Attachment( "PATH_TO_YOUR_FILE", MediaTypeNames.Application.Octet); // your path may look like Server.MapPath("~/file.ABC") message.Attachments.Add(data); 

使用文件名创建Attachment类的对象,并将其添加到消息的Attachments属性中

  Attachment attachment = new Attachment("file.ext"); message.Attachments.Add(attachment); 

 public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg) { try { // Create the mail message MailMessage objMailMsg = new MailMessage(strFrom, strTo); objMailMsg.BodyEncoding = Encoding.UTF8; objMailMsg.Subject = strSubject; objMailMsg.Body = strMsg; Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc")); objMailMsg.Attachments.Add(at); objMailMsg.Priority = MailPriority.High; objMailMsg.IsBodyHtml = true; //prepare to send mail via SMTP transport SmtpClient objSMTPClient = new SmtpClient(); objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; objSMTPClient.Send(objMailMsg); return true; } catch (Exception ex) { throw ex; } } 

参考

这是一个代码……

 public void MailSend(string strfrom, string strto, string strSubject, string strBody, string resumename, string sresumename) { try { MailMessage msg = new MailMessage(strfrom, strto);// strEmail); msg.Bcc.Add("xx@xxxx.com"); msg.Body = strBody; msg.Subject = strSubject; msg.IsBodyHtml = true; if (resumename.Length > 0) { Attachment att = new Attachment(Server.MapPath(VirtualPathUtility.ToAbsolute("~/User_Resume/" + resumename))); msg.Attachments.Add(att); } if (sresumename.Length > 0) { Attachment att1 = new Attachment(Server.MapPath(VirtualPathUtility.ToAbsolute("~/User_Resume/" + sresumename))); msg.Attachments.Add(att1); } System.Net.Mail.SmtpClient cli = new System.Net.Mail.SmtpClient("111.111.111.111",25); cli.Credentials = new NetworkCredential("nnnnnnn", "yyyyyy"); cli.Send(msg); msg.Dispose(); ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Enquiery submitted sucessfully');", true); } catch (Exception ex) { ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('"+ex.Message+"');", true); } } 

使用简单的编码在ASP.Net中发送带附件的电子邮件。 在本文中,我将向您展示如何执行此操作。

的Index.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="_Default" Debug="true" %>    Careers   

Index.aspx.cs

 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Mail; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void bttn_Send_Click(object sender, EventArgs e) { string from = “info@suryarpraveen-wordpress.com”; string textTo = “careers@suryarpraveen-wordpress.com”; using (MailMessage mail = new MailMessage(from, textTo)) { mail.Subject = “Careers – Surya R Praveen WordPress”; mail.Body = string.Format(@” Name: {0} Email: {1} Contact: {2} Job: {3} Experience: {4} “, txtName.Text, txtEmail.Text, txtcontact.Text, txtjobTitle.SelectedItem.Text, txtjobExp.SelectedItem.Text); if (fileUploader.HasFile) { string fileName = Path.GetFileName(fileUploader.PostedFile.FileName); mail.Attachments.Add(new Attachment(fileUploader.PostedFile.InputStream, fileName)); } mail.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.Host = “mail.suryarpraveen-wordpress.com”; smtp.EnableSsl = false; NetworkCredential networkCredential = new NetworkCredential(from, “password@007”); smtp.UseDefaultCredentials = true; smtp.Credentials = networkCredential; smtp.Port = 25; smtp.Send(mail); ClientScript.RegisterStartupScript(GetType(), “alert”, “alert('Message has been sent successfully.');”, true); } } } 

https://suryarpraveen.wordpress.com/2017/08/22/how-to-send-email-with-attachment-in-asp-net/

上述就是C#学习教程:如何在Asp.Net中发送带附件的电子邮件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 protected void Send_Button_Click(object sender, EventArgs e) { MailMessage mail = new MailMessage(); mail.From = new MailAddress("One@gmail.com"); mail.To.Add(new MailAddress("Two@yahoo.com")); mail.Bcc.Add(new MailAddress("Three@gmail.com")); mail.Subject = "Testing E-mail By ASP.NET"; mail.Body = "This is only for Demo "; if (FileUpload1.HasFile) { Attachment attach = new Attachment(FileUpload1.PostedFile.InputStream ,FileUpload1.PostedFile.FileName); mail.Attachments.Add(attach); } SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; System.Net.NetworkCredential credential = new System.Net.NetworkCredential(); credential.UserName = "One@gmail.com"; credential.Password = "123456789"; smtp.Credentials = credential; smtp.EnableSsl = true; smtp.Send(mail); } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1031785.html

发表评论

邮箱地址不会被公开。 必填项已用*标注