首页 > 代码库 > C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
net2.0后,C#可以利用System.Net发送邮件了。
代码整理如下:
3. 增加IProcessMessage类,定义了一个消息方法,用于消息传递
1 /************************************** 2 * <copyright file="SendEail.cs"> 3 * Copyright(c)2014 LeeStudio All rights reserved. 4 * CLR版本:4.0.30319.17929 5 * 开发团队:欺心伴月个人工作室 6 * 技术博客:http://www.cnblogs.com/lichunyang/ 7 * 所属工程:LeeStudio.Basic 8 * 创 建 者:Frank Lee 9 * 修改时间:2014-02-20 11:20 10 * </copyright> 11 **************************************/ 12 13 using System; 14 using System.Text; 15 using System.Net; 16 using System.Net.Mail; 17 using System.Net.Mime; 18 19 namespace LeeStudio.Basic 20 { 21 /// <summary> 22 /// 邮件发送类 23 /// </summary> 24 public class SendEmail 25 { 26 private MailMessage mailMessage = new MailMessage(); //实例化一个邮件类 27 28 #region 构造函数 29 /// <summary> 30 /// 构造函数 31 /// </summary> 32 /// <param name="toAddresses">收件人地址(多个以,号分开)</param> 33 /// <param name="fromAddress">发件人地址</param> 34 /// <param name="title">主题</param> 35 /// <param name="body">正文</param> 36 public SendEmail(string toAddresses, string fromAddress, string title, string body) 37 : this(toAddresses, fromAddress, "", "", title, body, false) 38 { 39 } 40 41 /// <summary> 42 /// 构造函数 43 /// </summary> 44 /// <param name="toAddress">收件人地址</param> 45 /// <param name="fromAddress">发件人地址</param> 46 /// <param name="toName">收件人名字</param> 47 /// <param name="fromName">发件人姓名</param> 48 /// <param name="title">主题</param> 49 /// <param name="body">正文</param> 50 /// <param name="isBodyHtml">正文是否为html格式</param> 51 public SendEmail(string toAddress, string fromAddress, string toName, string fromName, string title, string body, bool isBodyHtml) 52 { 53 mailMessage.From = new MailAddress(fromAddress, fromName, Encoding.GetEncoding(936)); 54 if (toName.Equals("")) 55 mailMessage.To.Add(toAddress); 56 else 57 mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936))); 58 59 mailMessage.Subject = title; 60 mailMessage.SubjectEncoding = Encoding.GetEncoding(936); 61 62 mailMessage.Body = body; 63 mailMessage.IsBodyHtml = isBodyHtml; 64 mailMessage.BodyEncoding = Encoding.GetEncoding(936); 65 } 66 #endregion 67 68 /// <summary> 69 /// 设置SMTP,并且将邮件发送出去 70 /// 所有参数都设置完成后再调用该方法 71 /// </summary> 72 /// <param name="password">发件人密码</param> 73 /// <param name="smtpHost">SMTP服务器地址</param> 74 public void SetSmtp(string password, string smtpHost) 75 { 76 SetSmtp(mailMessage.From.Address, password, smtpHost, 25, false, MailPriority.Normal); 77 } 78 79 /// <summary> 80 /// 设置SMTP,并且将邮件发送出去 81 /// 所有参数都设置完成后再调用该方法 82 /// </summary> 83 /// <param name="address">发件人地址(必须为真实有效的email地址)</param> 84 /// <param name="password">发件人密码</param> 85 /// <param name="smtpHost">SMTP服务器地址</param> 86 /// <param name="smtpPort">SMTP服务器的端口</param> 87 /// <param name="isEnableSsl">SMTP服务器是否启用SSL加密</param> 88 /// <param name="priority">邮件的优先级</param> 89 public void SetSmtp(string address, string password, string smtpHost, int smtpPort, bool isEnableSsl, MailPriority priority) 90 { 91 SmtpClient smtp = new SmtpClient(); 92 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 93 smtp.Credentials = new NetworkCredential(address, password); 94 smtp.Host = smtpHost; 95 smtp.Port = smtpPort; 96 smtp.EnableSsl = isEnableSsl; 97 98 mailMessage.Priority = priority; 99 smtp.Send(mailMessage); //发送邮件 100 } 101 102 #region //设置邮件地址 103 /// <summary> 104 /// 设置更多收件人 105 /// </summary> 106 /// <param name="toAddresses">收件人地址</param> 107 public void SetMoreToAddress(string toAddresses) 108 { 109 mailMessage.To.Add(toAddresses); 110 } 111 112 /// <summary> 113 /// 设置更多收件人 114 /// </summary> 115 /// <param name="toAddresses">收件人地址</param> 116 /// <param name="toName">收件人名字</param> 117 public void SetMoreToAddress(string toAddress, string toName) 118 { 119 mailMessage.To.Add(new MailAddress(toAddress, toName, Encoding.GetEncoding(936))); 120 } 121 122 /// <summary> 123 /// 设置抄送者(多个以,号分开) 124 /// </summary> 125 /// <param name="ccAddresses">抄送者地址</param> 126 public void SetCarbonCopyFor(string ccAddresses) 127 { 128 mailMessage.CC.Add(ccAddresses); 129 } 130 131 /// <summary> 132 /// 设置抄送者 133 /// </summary> 134 /// <param name="ccAddress">抄送者地址</param> 135 /// <param name="ccName">抄送者名字</param> 136 public void SetCarbonCopyFor(string ccAddress, string ccName) 137 { 138 mailMessage.Bcc.Add(new MailAddress(ccAddress, ccName, Encoding.GetEncoding(936))); 139 } 140 141 /// <summary> 142 /// 设置密送者(多个以,号分开) 143 /// </summary> 144 /// <param name="bccAddresses">密送者</param> 145 public void SetBlindCarbonCopyFor(string bccAddresses) 146 { 147 mailMessage.Bcc.Add(bccAddresses); 148 } 149 150 /// <summary> 151 /// 设置密送者 152 /// </summary> 153 /// <param name="bccAddress">密送者</param> 154 /// <param name="bccName">密送者名字</param> 155 public void SetBlindCarbonCopyFor(string bccAddress, string bccName) 156 { 157 mailMessage.Bcc.Add(new MailAddress(bccAddress, bccName, Encoding.GetEncoding(936))); 158 } 159 #endregion 160 161 #region 添加附件 162 /// <summary> 163 /// 添加附件(自动识别文件类型) 164 /// </summary> 165 /// <param name="fileName">单个文件的路径</param> 166 public void Attachments(string fileName) 167 { 168 mailMessage.Attachments.Add(new Attachment(fileName)); 169 } 170 171 /// <summary> 172 /// 添加附件(默认为富文本RTF格式) 173 /// </summary> 174 /// <param name="fileName">单个文件的路径</param> 175 public void AttachmentsForRTF(string fileName) 176 { 177 mailMessage.Attachments.Add(new Attachment(fileName, MediaTypeNames.Application.Rtf)); 178 } 179 #endregion 180 } 181 }
调用是按照需求,自由设置参数。
1 using LeeStudio.Basic; 2 3 private void SendEmail() 4 { 5 #region 最简设置 6 //SendEmail se = new SendEmail("*****@****.com", "****@163.com", "title", "body"); 7 //se.SetSmtp("password", "smtp.163.com"); //必须在所有参数设置完后调用此方法 8 #endregion 9 10 #region 完全设置 11 //string toAddresses = "***@**.**,***@**.**"; 12 //string toNames = "frank,lee"; 13 14 //SendEmail se = new SendEmail("to@**.**", "from@**.**", "toname", "fromname","title", "body", false); 15 //se.SetMoreToAddress("to@**.**", "toName"); 16 //se.SetCarbonCopyFor("cc@**.**"); 17 //se.SetBlindCarbonCopyFor("bcc@**.**"); 18 19 //string filePath = Request.PhysicalApplicationPath + "/file/SendEmail.txt"; 20 //se.Attachments(filePath); 21 //filePath = Request.PhysicalApplicationPath + "/file/RTF文件.docx"; 22 //se.AttachmentsForRTF(filePath); 23 //se.SetSmtp("email", "password", "smtp", 25, true, System.Net.Mail.MailPriority.Normal); //必须在所有参数设置完后调用此方法 24 #endregion 25 }
遗留问题:
当有多个收件人/抄送者/密送者时,如何快速处理?之前的做法是让用户输入字符串,再将字符串一一对应匹配,代码如下:
1 /// <summary> 2 /// 将邮件地址和名称加入到锯齿数组中 3 /// </summary> 4 /// <param name="addrs">邮件地址(多个以,号分开)</param> 5 /// <param name="names">名称(多个以,号分开)</param> 6 /// <returns>地址和名称数组</returns> 7 private string[][] SetNameList(string addrs, string names) 8 { 9 string[][] result = new string[2][]; 10 11 string[] addr = addrs.Split(‘,‘); 12 string[] name = names.Split(‘,‘); 13 result[0] = new string[addr.Length]; 14 result[1] = new string[name.Length]; 15 16 for (int i = 0; i < addr.Length; i++) 17 result[0][i] = addr[i]; 18 for (int i = 0; i < name.Length; i++) 19 result[1][i] = name[i]; 20 21 return result; 22 }
首先这个方法就很奇葩,其次,只要用户输入的个数不对应,又要做一堆的异常处理。
C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。