?1 using System; ?2 using System.Text; ?3 using System.Net.Mail; ?4 using System.Net; ?5 using System.Linq; ?6 using System.Text.RegularExpressions; ?7 ??8 namespace Dos.ORM.Common.Helpers ?9 { 10 ????/// <summary> 11 ????/// 发送邮件帮助类 12 ????/// </summary> 13 ????public class EmailHelper 14 ????{ 15 ?16 ????????private readonly SmtpClient _smtp = new SmtpClient(); 17 ????????private Encoding Encoding { get; set; } 18 ????????private bool IsHtml { get; set; } 19 ????????private string[] Cc { get; set; } 20 ????????private string[] Bcc { get; set; } 21 ?22 ????????/// <summary> 23 ????????/// 是否为腾讯企业邮箱 24 ????????/// </summary> 25 ????????private readonly bool _isExmailQq; 26 ?27 ????????/// <summary> 28 ????????/// 发件邮箱密码或授权码 29 ????????/// </summary> 30 ????????private string Password { get; set; } 31 ?32 ????????/// <summary> 33 ????????/// 获取发送结果,成功则为空 34 ????????/// </summary> 35 ????????public string Result { get; private set; } 36 ?37 ????????/// <summary> 38 ????????/// 设置邮件编码类型 39 ????????/// </summary> 40 ????????/// <param name="contentEncoding"></param> 41 ????????public void SetEncoding(Encoding contentEncoding) 42 ????????{ 43 ????????????Encoding = contentEncoding; 44 ?45 ????????} 46 ????????/// <summary> 47 ????????///设置邮件正文是否为 Html 格式 ?48 ????????/// </summary> 49 ????????/// <param name="isHtml"></param> 50 ????????public void SetIsHtml(bool isHtml) 51 ????????{ 52 ????????????IsHtml = isHtml; 53 ????????} 54 ????????/// <summary> 55 ????????/// 抄送 56 ????????/// </summary> 57 ????????/// <param name="cc"></param> 58 ????????public void SetCc(params string[] cc) 59 ????????{ 60 ????????????Cc = cc; 61 ????????} 62 ?63 ????????/// <summary> 64 ????????/// 暗送 65 ????????/// </summary> 66 ????????/// <param name="bc"></param> 67 ????????public void SetBc(params string[] bc) 68 ????????{ 69 ????????????Bcc = bc; 70 ????????} 71 ????????/// <summary> 72 ????????/// 是否ssl加密 73 ????????/// </summary> 74 ????????/// <param name="isSsl"></param> 75 ????????public void SetIsSsl(bool isSsl) 76 ????????{ 77 ????????????_smtp.EnableSsl = isSsl; 78 ????????} 79 ?80 ????????/// <summary> 81 ????????/// 构造函数 82 ????????/// </summary> 83 ????????/// <param name="host">SMTP事务的主机的名称或IP地址</param> 84 ????????/// <param name="sendUserAccName">发件人邮件账号名称(不包含@及后缀)</param> 85 ????????/// <param name="sendUserPwd">发件人邮箱密码</param> 86 ????????/// <param name="isExmailQq">是否为腾讯企业邮箱</param> 87 ????????/// <param name="port">端口号,默认为25</param> 88 ????????public EmailHelper(string host, string sendUserAccName, string sendUserPwd, bool isExmailQq = false, int port = 25) 89 ????????{ 90 ????????????_smtp.Host = host; 91 ????????????Password = sendUserPwd; 92 ????????????_isExmailQq = isExmailQq; 93 ????????????_smtp.Port = port != 25 ? port : 0x19; 94 ????????????_smtp.EnableSsl = false; 95 ?96 ????????????IsHtml = true; 97 ????????????Encoding = Encoding.UTF8; 98 ?99 ????????????if (string.IsNullOrEmpty(sendUserAccName) || string.IsNullOrEmpty(sendUserPwd))100 ????????????{101 ????????????????_smtp.UseDefaultCredentials = false;102 ????????????}103 ????????????else104 ????????????{105 ????????????????_smtp.Credentials = new NetworkCredential(sendUserAccName, sendUserPwd);106 ????????????}107 ????????}108 109 ????????/// <summary>110 ????????/// 发送邮件111 ????????/// </summary>112 ????????/// <param name="fromAcc">发件人邮件地址</param>113 ????????/// <param name="fromUserNickname">发件人显示名称</param>114 ????????/// <param name="toUserAcc">收件人地址</param>115 ????????/// <param name="title">邮件标题</param>116 ????????/// <param name="body">邮件正文</param>117 ????????/// <param name="file">附件地址数组</param>118 ????????/// <returns>bool 是否成功 </returns>119 ????????public bool Send(string fromAcc, string fromUserNickname, string toUserAcc, string title, string body, params string[] file)120 ????????{121 ????????????return Send(fromAcc, fromUserNickname, new string[] { toUserAcc }, title, body, file);122 ????????}123 124 ????????/// <summary>125 ????????/// 发送邮件126 ????????/// </summary>127 ????????/// <param name="fromAcc">发件人邮件地址</param>128 ????????/// <param name="fromUserNickname">发件人显示名称</param>129 ????????/// <param name="toUserAcc">收件人地址</param>130 ????????/// <param name="title">邮件标题</param>131 ????????/// <param name="body">邮件正文</param>132 ????????/// <param name="file">附件地址数组</param>133 ????????/// <returns>bool 是否成功 </returns>134 ????????public bool Send(string fromAcc, string fromUserNickname, string[] toUserAcc, string title, string body, params string[] file)135 ????????{136 ????????????string mailReg = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";137 ????????????if (toUserAcc == null)138 ????????????{139 ????????????????throw new ArgumentNullException("EmailHelper.Send.to");140 ????????????}141 142 ????????????if (toUserAcc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))143 ????????????{144 ????????????????Result = "收件人地址不合法";145 ????????????????return false;146 ????????????}147 ????????????if (Bcc != null && Bcc.Length > 0)148 ????????????{149 ????????????????if (Bcc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))150 ????????????????{151 ????????????????????Result = "暗送人地址不合法";152 ????????????????????return false;153 ????????????????}154 ????????????}155 ????????????if (Cc != null && Cc.Length > 0)156 ????????????{157 ????????????????if (Cc.Any(oit => !Regex.IsMatch(oit + "", mailReg)))158 ????????????????{159 ????????????????????Result = "抄送人地址不合法";160 ????????????????????return false;161 ????????????????}162 ????????????}163 164 ????????????if (_isExmailQq)165 ????????????{166 ????????????????#region 检查是否为腾讯企业邮箱(注意这里有一个坑,腾旭企业邮箱需要单独进行以下设置)167 ????????????????//此坑可参见:https://www.cnblogs.com/gxivwshjj/p/7562311.html168 169 ????????????????var mail = new System.Web.Mail.MailMessage();170 ????????????????try171 ????????????????{172 ????????????????????mail.To = string.Join(";", toUserAcc);173 ????????????????????mail.From = fromAcc;174 ????????????????????mail.Subject = title;175 ????????????????????mail.BodyFormat = System.Web.Mail.MailFormat.Html;176 ????????????????????mail.Body = body;177 178 ????????????????????//设置基础身份验证为1179 ????????????????????mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");180 ????????????????????//设置发件邮箱地址181 ????????????????????mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", fromAcc);182 ????????????????????//设置发件邮箱密码183 ????????????????????mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password);184 ????????????????????//设置端口号为465185 ????????????????????mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);186 ????????????????????//设置SSL为true187 ????????????????????mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");188 189 ????????????????????//发送邮件的附件190 ????????????????????foreach (var r in file)191 ????????????????????{192 ????????????????????????var objMailAttachment = new Attachment(r);193 ????????????????????????mail.Attachments.Add(objMailAttachment);194 ????????????????????}195 196 ????????????????????System.Web.Mail.SmtpMail.SmtpServer = _smtp.Host;197 ????????????????????System.Web.Mail.SmtpMail.Send(mail);198 199 ????????????????????return true;200 ????????????????}201 ????????????????catch (Exception ex)202 ????????????????{203 ????????????????????Result = ex.Message;204 ????????????????}205 206 ????????????????#endregion207 ????????????}208 ????????????else209 ????????????{210 ????????????????MailMessage message = new MailMessage();211 212 ????????????????//创建一个附件对象213 ????????????????foreach (var r in file)214 ????????????????{215 ????????????????????//发送邮件的附件216 ????????????????????var objMailAttachment = new Attachment(r);217 ????????????????????message.Attachments.Add(objMailAttachment);218 ????????????????}219 ????????????????message.From = new MailAddress(fromAcc, fromUserNickname);220 ????????????????message.Subject = title;221 ????????????????message.SubjectEncoding = Encoding;222 ????????????????message.Body = body;223 ????????????????message.BodyEncoding = Encoding;224 ????????????????message.IsBodyHtml = IsHtml;225 ????????????????message.Priority = MailPriority.Normal;226 ????????????????foreach (string str in toUserAcc)227 ????????????????{228 ????????????????????message.To.Add(str);229 ????????????????}230 ????????????????if (Bcc != null && Bcc.Length > 0)231 ????????????????{232 ????????????????????foreach (string b in Bcc)233 ????????????????????{234 ????????????????????????message.Bcc.Add(b);235 ????????????????????}236 ????????????????}237 ????????????????if (Cc != null && Cc.Length > 0)238 ????????????????{239 ????????????????????foreach (string c in Cc)240 ????????????????????{241 ????????????????????????message.CC.Add(c);242 ????????????????????}243 ????????????????}244 245 ????????????????try246 ????????????????{247 ????????????????????_smtp.Send(message);248 ????????????????????return true;249 ????????????????}250 ????????????????catch (Exception ex)251 ????????????????{252 ????????????????????Result = ex.Message;253 ????????????????}254 ????????????}255 256 ????????????return false;257 ????????}258 ????}259 }
.Net Email操作类
原文地址:http://www.cnblogs.com/qubernet/p/8058620.html