C# 使用系统方法发送异步邮件
项目背景:
最近在对几年前的一个项目进行重构,发现发送邮件功能需要一定的时间来处理,而由于发送是同步的因此导致在发送邮件时无法执行后续的操作
实际上发送邮件后只需要将发送结果写入系统日志即可对其他业务没有任何影响,因此决定将发送邮件操作更改为异步的
由于使用的是C#的邮件类库,而C#本身已经提供了异步发送的功能即只需要将Send方法更改为SendAsync即可,更改方法名并不难但发送后再写入日志就有点难了
因为项目中发送邮件是单独的组件,所以我不可能在发送邮件类库中直接添加写入日志操作(不在同一个类库,网络和MSDN上的例子都是同一组件下)
但C#可以使用委托将方法作为参数来传递的,因此我就可以在发送邮件的方法中添加一个回调方法,在异步发送邮件后再执行回调方法即可
完整代码:
/****************************************************************** * 创建人:HTL * 创建时间:2015-04-16 21:34:25 * 说明:C# 发送异步邮件Demo * Email:huangyuan413026@163.com *******************************************************************/ using System; using System.Net.Mail; namespace SendAsyncEmailTest { class Program { const string dateFormat = "yyyy-MM-dd :HH:mm:ss:ffffff"; static void Main(string[] args) { Console.WriteLine("开始异步发送邮件,时间:" + DateTime.Now.ToString(dateFormat)); new MailHelper().SendAsync("Send Async Email Test", "This is Send Async Email Test", "huangyuan413026@163.com", emailCompleted); Console.WriteLine("邮件正在异步发送,时间:" + DateTime.Now.ToString(dateFormat)); Console.ReadKey(); Console.WriteLine(); } /// <summary> /// 邮件发送后的回调方法 /// </summary> /// <param name="message"></param> static void emailCompleted(string message) { //延时1秒 System.Threading.Thread.Sleep(1000); Console.WriteLine(); Console.WriteLine("邮件发送结果:\r\n" + (message == "true" ? "邮件发送成功" : "邮件发送失败") + ",时间:" + DateTime.Now.ToString(dateFormat)); //写入日志 } } /// <summary> /// 发送邮件类 /// </summary> public class MailHelper { public delegate int MethodDelegate(int x, int y); private readonly int smtpPort = 25; readonly string SmtpServer = "smtp.baidu.com"; private readonly string UserName = "support@baidu.com"; readonly string Pwd = "baidu.com"; private readonly string AuthorName = "BaiDu"; public string Subject { get; set; } public string Body { get; set; } public string Tos { get; set; } public bool EnableSsl { get; set; } MailMessage GetClient { get { if (string.IsNullOrEmpty(Tos)) return null; MailMessage mailMessage = new MailMessage(); //多个接收者 foreach (string _str in Tos.Split(',')) { mailMessage.To.Add(_str); } mailMessage.From = new System.Net.Mail.MailAddress(UserName, AuthorName); mailMessage.Subject = Subject; mailMessage.Body = Body; mailMessage.IsBodyHtml = true; mailMessage.BodyEncoding = System.Text.Encoding.UTF8; mailMessage.SubjectEncoding = System.Text.Encoding.UTF8; mailMessage.Priority = System.Net.Mail.MailPriority.High; return mailMessage; } } SmtpClient GetSmtpClient { get { return new SmtpClient { UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential(UserName, Pwd), DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network, Host = SmtpServer, Port = smtpPort, EnableSsl = EnableSsl, }; } } //回调方法 Action<string> actionSendCompletedCallback = null; ///// <summary> ///// 使用异步发送邮件 ///// </summary> ///// <param name="subject">主题</param> ///// <param name="body">内容</param> ///// <param name="to">接收者,以,分隔多个接收者</param> //// <param name="_actinCompletedCallback">邮件发送后的回调方法</param> ///// <returns></returns> public void SendAsync(string subject, string body, string to, Action<string> _actinCompletedCallback) { if (string.IsNullOrEmpty(to)) return; Tos = to; SmtpClient smtpClient = GetSmtpClient; MailMessage mailMessage = GetClient; if (smtpClient == null || mailMessage == null) return; Subject = subject; Body = body; EnableSsl = false; //发送邮件回调方法 actionSendCompletedCallback = _actinCompletedCallback; smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback); try { smtpClient.SendAsync(mailMessage, "true");//异步发送邮件,如果回调方法中参数不为"true"则表示发送失败 } catch (Exception e) { throw new Exception(e.Message); } finally { smtpClient = null; mailMessage = null; } } /// <summary> /// 异步操作完成后执行回调方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { //同一组件下不需要回调方法,直接在此写入日志即可 //写入日志 //return; if (actionSendCompletedCallback == null) return; string message = string.Empty; if (e.Cancelled) { message = "异步操作取消"; } else if (e.Error != null) { message = (string.Format("UserState:{0},Message:{1}", (string)e.UserState, e.Error.ToString())); } else message = (string)e.UserState; //执行回调方法 actionSendCompletedCallback(message); } } }
有图有真相