C# 发送 Email 功能:EmailHelper(群发,多附件)
第一篇文章,没想会是这个 EmailHelper。
最近在总结一般的网站建设中典型的模块和功能,今天下午到了 Email 这块,上网找了半天也没找到几个,不知道是太简单还是怎么回事。
好不容易搜了个出来,漏洞百出,一发就报异常,干脆自己写一个。
测试接收的邮箱有 163、126、qq、gmail、hotmail、sina,都没问题,可以多收件人、多附件发送。
另外,我在写的时候出了个小错误,在这里也提个醒:发件人邮箱的 smtp 服务必须开启才能发送成功。
还有不完善的地方,会继续改进。
下面贴代码:
1 using System; 2 using System.Net; 3 using System.Net.Mail; 4 using System.Text; 5 6 namespace Polin.Utility 7 { 8 public class EmailHelper 9 { 10 #region Constructor 11 12 public EmailHelper() 13 { } 14 15 #endregion 16 17 #region Private Fields 18 19 private string _mailSubject; 20 private string _mailBody; 21 private string _mailFrom; 22 private string _mailFromName; 23 private string _mailTo; 24 private string _smtp; 25 private int _smtpPort; 26 private string _userName; 27 private string _pwd; 28 private MailPriority _mailPrioty; 29 private string file; 30 31 #endregion 32 33 #region Public Properties 34 35 /// <summary> 36 /// 邮件主题,默认为 “无主题” 37 /// </summary> 38 public string MailSubject 39 { 40 get 41 { 42 if (string.IsNullOrEmpty(this._mailSubject)) 43 { 44 return "无主题"; 45 } 46 return this._mailSubject; 47 } 48 set { _mailSubject = value; } 49 } 50 51 /// <summary> 52 /// 邮件内容 53 /// </summary> 54 public string MailBody 55 { 56 get { return _mailBody; } 57 set { _mailBody = value; } 58 } 59 60 /// <summary> 61 /// 发件人地址(必填) 62 /// </summary> 63 public string MailFrom 64 { 65 get { return _mailFrom; } 66 set { _mailFrom = value; } 67 } 68 69 /// <summary> 70 /// 发件人名称,默认为 “系统邮件” 71 /// </summary> 72 public string MailFromName 73 { 74 get 75 { 76 if (string.IsNullOrEmpty(this._mailFromName)) 77 { 78 return "系统邮件"; 79 } 80 return this._mailFromName; 81 } 82 set { _mailFromName = value; } 83 } 84 85 /// <summary> 86 /// 收件人地址,多个收件人地址用逗号隔开(必填) 87 /// </summary> 88 public string MailTo 89 { 90 get { return _mailTo; } 91 set { _mailTo = value; } 92 } 93 94 /// <summary> 95 /// smtp 地址,如 smtp.163.com(必填) 96 /// </summary> 97 public string Smtp 98 { 99 get { return _smtp; } 100 set { _smtp = value; } 101 } 102 103 /// <summary> 104 /// smtp 端口,默认为 25 105 /// </summary> 106 public int SmtpPort 107 { 108 get 109 { 110 if (this._smtpPort == 0) 111 { 112 return 25; 113 } 114 return this._smtpPort; 115 } 116 set { _smtpPort = value; } 117 } 118 119 /// <summary> 120 /// 发件人账户,如 abc@163.com,则账户为 abc(必填) 121 /// </summary> 122 public string UserName 123 { 124 get { return _userName; } 125 set { _userName = value; } 126 } 127 128 /// <summary> 129 /// 发件人密码(必填) 130 /// </summary> 131 public string Pwd 132 { 133 get { return _pwd; } 134 set { _pwd = value; } 135 } 136 137 /// <summary> 138 /// 邮件等级:低、正常、紧急,默认为 正常 139 /// </summary> 140 public MailPriority MailPrioty 141 { 142 get 143 { 144 if (this._mailPrioty != MailPriority.Low && this._mailPrioty != MailPriority.High && this._mailPrioty != MailPriority.Normal) 145 { 146 return MailPriority.Normal; 147 } 148 return this._mailPrioty; 149 } 150 set { _mailPrioty = value; } 151 } 152 153 /// <summary> 154 /// 附件地址,多个地址用逗号隔开。Web 应用中请使用 Server.MapPath() 映射到实际地址 155 /// </summary> 156 public string File 157 { 158 get { return file; } 159 set { file = value; } 160 } 161 162 #endregion 163 164 #region Method 165 166 /// <summary> 167 /// 发送邮件 168 /// </summary> 169 /// <param name="error">输出参数:发生错误时返回错误信息</param> 170 /// <returns>是否发送成功</returns> 171 public bool SendMail(out string error) 172 { 173 error = string.Empty; 174 if (!Valid(out error)) 175 { 176 return false; 177 } 178 MailAddress mailFrom = new MailAddress(this.MailFrom, this.MailFromName, Encoding.UTF8); 179 MailMessage mail = new MailMessage(); 180 mail.From = mailFrom; 181 string[] mailToCollection = this.MailTo.Split(','); 182 foreach (string s in mailToCollection) 183 { 184 MailAddress t = new MailAddress(s, "", Encoding.UTF8); 185 mail.To.Add(t); 186 } 187 if (!string.IsNullOrEmpty(this.File)) 188 { 189 string[] fileCollection = this.File.Split(','); 190 foreach (string f in fileCollection) 191 { 192 Attachment t = new Attachment(f); 193 mail.Attachments.Add(t); 194 } 195 } 196 mail.Subject = this.MailSubject; 197 mail.SubjectEncoding = Encoding.UTF8; 198 mail.Body = this.MailBody; 199 mail.BodyEncoding = Encoding.UTF8; 200 mail.IsBodyHtml = true; 201 202 SmtpClient smtp = new SmtpClient(); 203 smtp.Host = this.Smtp; 204 smtp.Port = this.SmtpPort; 205 smtp.Credentials = new NetworkCredential(this.UserName, this.Pwd); 206 try 207 { 208 smtp.Send(mail); 209 return true; 210 } 211 catch (Exception e) 212 { 213 error = e.Message; 214 return false; 215 } 216 } 217 218 /// <summary> 219 /// 检查必填属性 220 /// </summary> 221 /// <param name="e">输出错误信息</param> 222 /// <returns>是否有效</returns> 223 private bool Valid(out string e) 224 { 225 e = string.Empty; 226 if (string.IsNullOrEmpty(this._mailFrom)) 227 { 228 e = "发件人地址不能为空"; 229 return false; 230 } 231 if (string.IsNullOrEmpty(this._mailTo)) 232 { 233 e = "收件人地址不能为空"; 234 return false; 235 } 236 if (string.IsNullOrEmpty(this._userName)) 237 { 238 e = "发件人账户名不能为空"; 239 return false; 240 } 241 if (string.IsNullOrEmpty(this._pwd)) 242 { 243 e = "发件人密码不能为空"; 244 return false; 245 } 246 if (string.IsNullOrEmpty(this._smtp)) 247 { 248 e = "smtp 地址不能为空"; 249 return false; 250 } 251 return true; 252 } 253 254 #endregion 255 } 256 }
本文来自博客园,作者:GPL-技术沉思录,转载请注明原文链接:https://www.cnblogs.com/polin/archive/2012/04/17/2454167.html