//获取邮件超时时间 private static int GetTimeoutMilliSeconds(MyMailMessage myMailMessage) { if (myMailMessage.TimeoutSeconds > 0) { return(myMailMessage.TimeoutSeconds * 1000); } else { return(300 * 1000); //5分钟 } }
private List <Attachment> GetAttachments(MyMailMessage myMailMessage) { List <Attachment> result = new List <Attachment>(); if (!string.IsNullOrEmpty(myMailMessage.Attachments)) { Regex regex = new Regex("<([^<>]+)>"); //包含在<...>之中的字符串 foreach (Match match in regex.Matches(myMailMessage.Attachments)) { Attachment attachment = GetAttachment(match); if (attachment != null) { result.Add(attachment); } } } return(result); }
public void SendEmail(MyMailMessage myMailMessage) { #region 获取并验证发送参数 myMailMessage.To = (myMailMessage.To == null ? string.Empty : myMailMessage.To.Trim()); myMailMessage.Cc = (myMailMessage.Cc == null ? string.Empty : myMailMessage.Cc.Trim()); myMailMessage.Bcc = (myMailMessage.Bcc == null ? string.Empty : myMailMessage.Bcc.Trim()); if (myMailMessage.To.Length == 0 && myMailMessage.Cc.Length == 0 && myMailMessage.Bcc.Length == 0) { throw new Exception("收件方邮件地址不能全为空!"); } string REG_EMAIL = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"; if (myMailMessage.To.Length > 0 && !Regex.IsMatch(myMailMessage.To, REG_EMAIL)) { throw new Exception("收件人邮件地址不正确!"); } if (myMailMessage.Cc.Length > 0 && !Regex.IsMatch(myMailMessage.Cc, REG_EMAIL)) { throw new Exception("抄送邮件地址不正确!"); } if (myMailMessage.Bcc.Length > 0 && !Regex.IsMatch(myMailMessage.Bcc, REG_EMAIL)) { throw new Exception("密送邮件地址不正确!"); } myMailMessage.To = myMailMessage.To.Replace(";", ",").TrimEnd(','); myMailMessage.Cc = myMailMessage.Cc.Replace(";", ",").TrimEnd(','); myMailMessage.Bcc = myMailMessage.Bcc.Replace(";", ",").TrimEnd(','); myMailMessage.Body = myMailMessage.Body + "\n\n"; #endregion #region 构造邮件消息并发送 MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(myMailMessage.ServiceAccount, myMailMessage.DisplayName); if (myMailMessage.To.Length > 0) { mailMessage.To.Add(myMailMessage.To); } if (myMailMessage.Cc.Length > 0) { mailMessage.CC.Add(myMailMessage.Cc); } if (myMailMessage.Bcc.Length > 0) { mailMessage.Bcc.Add(myMailMessage.Bcc); } mailMessage.Subject = myMailMessage.Subject; mailMessage.Body = myMailMessage.Body; mailMessage.SubjectEncoding = _Encoding; mailMessage.BodyEncoding = _Encoding; mailMessage.IsBodyHtml = true; List <Attachment> attachments = GetAttachments(myMailMessage); foreach (Attachment item in attachments) { mailMessage.Attachments.Add(item); } SmtpClient smtpClient = new SmtpClient(myMailMessage.SmtpServer); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Port = myMailMessage.Port; smtpClient.UseDefaultCredentials = true; smtpClient.Credentials = new NetworkCredential(myMailMessage.ServiceAccount, myMailMessage.ServiceAccountPwd); smtpClient.EnableSsl = myMailMessage.EnableSsl; smtpClient.Timeout = GetTimeoutMilliSeconds(myMailMessage); try { smtpClient.Send(mailMessage); } catch (Exception ex) { //写日志 LogUtil.Write(ex.ToString(), LogLevel.Error, "SendEmail"); } smtpClient.Dispose(); mailMessage.Dispose(); #endregion }