Email发送

  1 public class EmailConfig
  2 {
  3 public static readonly string ServerAddress = ConfigurationManager.AppSettings["ServerAddress"];
  4 public static readonly string ServerUserName = ConfigurationManager.AppSettings["ServerUserName"];
  5 public static readonly string ServerUserPass = ConfigurationManager.AppSettings["ServerUserPass"];
  6 public static readonly string ServerPort = ConfigurationManager.AppSettings["ServerPort"];
  7 public static readonly string Authentication = ConfigurationManager.AppSettings["Authentication"];
  8 public static readonly string Sender = ConfigurationManager.AppSettings["Sender"];
  9 
 10 }
 11 
 12 public class EmailExten
 13 {
 14 /// <summary>
 15 /// 日志
 16 /// </summary>
 17 public static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 18 
 19 
 20 #region 邮件发送组件
 21 /// <summary>
 22 /// 发送邮件 Created by ZhangQC 2016.08.23
 23 /// </summary>
 24 /// <param name="recipient">收件人</param>
 25 /// <param name="subject">主题</param>
 26 /// <param name="body">正文</param>
 27 /// <param name="isBodyHtml">指示邮件正文是否为html</param>
 28 /// <param name="encoding">编码</param>
 29 /// <param name="isAuthentication">是否需要凭证</param>
 30 /// <param name="files">附件</param>
 31 static void Send(string recipient, string subject, string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
 32 {
 33 //初始化邮件发送
 34 var smtpClient = new SmtpClient
 35 {
 36 Host = EmailConfig.ServerAddress,
 37 Port = EmailConfig.ServerPort.ToInt(),
 38 Timeout = 3000,
 39 EnableSsl=true
 40 };
 41 //初始化消息
 42 var message = new MailMessage(EmailConfig.Sender, recipient)
 43 {
 44 IsBodyHtml = isBodyHtml,
 45 SubjectEncoding = encoding,
 46 BodyEncoding = encoding,
 47 Subject = subject,
 48 Body = body
 49 };
 50 //增加附件
 51 message.Attachments.Clear();
 52 if (files != null && files.Length != 0)
 53 {
 54 for (int i = 0; i < files.Length; ++i)
 55 {
 56 var attach = new Attachment(files[i]);
 57 message.Attachments.Add(attach);
 58 }
 59 }
 60 //用户名密码
 61 if (isAuthentication)
 62 {
 63 smtpClient.Credentials = new NetworkCredential(EmailConfig.ServerUserName, EmailConfig.ServerUserPass);
 64 }
 65 
 66 
 67 //发送完成回调
 68 smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClientSendCompleted);
 69 //object mailSendState = message;
 70 //smtpClient.SendAsync(message, mailSendState);
 71 //smtpClient.SendAsync(message,"ok");
 72 smtpClient.Send(message);
 73 
 74 
 75 }
 76 
 77 /// <summary>
 78 /// 发送回调
 79 /// </summary>
 80 /// <param name="sender"></param>
 81 /// <param name="e"></param>
 82 static void SmtpClientSendCompleted(object sender, AsyncCompletedEventArgs e)
 83 {
 84 bool mailSent = true;
 85 string state = (string)e.UserState;
 86 
 87 if (e.Cancelled)
 88 {
 89 mailSent = false;
 90 }
 91 if (e.Error != null)
 92 {
 93 mailSent = false;
 94 }
 95 
 96 }
 97 
 98 /// <summary>
 99 /// 邮件发送 Created by ZhangQC 2016.08.23
100 /// </summary>
101 /// <param name="recipient">收件人</param>
102 /// <param name="subject">主题</param>
103 /// <param name="body">内容</param>
104 public static bool Send(string recipient, string subject, string body)
105 {
106 if (string.IsNullOrEmpty(recipient))
107 return false;
108 try
109 {
110 Send(recipient, subject, body, true, Encoding.UTF8, true, null);
111 return true;
112 }
113 catch (Exception ex)
114 {
115 Log.ErrorFormat("邮件发送失败:{0}",ex);
116 return false;
117 }
118 }
119 
120 static void Send(string recipient, string sender, string subject, string body)
121 {
122 if (string.IsNullOrEmpty(recipient))
123 return;
124 
125 Send(recipient, subject, body, true, Encoding.UTF8, true, null);
126 }
127 #endregion
128 }
原文地址:https://www.cnblogs.com/creater/p/6322078.html