Email.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Net.Mail;
  3. namespace Masuit.Tools.Models
  4. {
  5. #pragma warning disable 1591
  6. public class Email
  7. {
  8. /// <summary>
  9. /// 发件人用户名
  10. /// </summary>
  11. public string Username { get; set; }
  12. /// <summary>
  13. /// 发件人邮箱密码
  14. /// </summary>
  15. public string Password { get; set; }
  16. /// <summary>
  17. /// 发送服务器端口号,默认25
  18. /// </summary>
  19. public int SmtpPort { get; set; } = 25;
  20. /// <summary>
  21. /// 发送服务器地址
  22. /// </summary>
  23. public string SmtpServer { get; set; }
  24. /// <summary>
  25. /// 邮件标题
  26. /// </summary>
  27. public string Subject { get; set; }
  28. /// <summary>
  29. /// 邮件正文
  30. /// </summary>
  31. public string Body { get; set; }
  32. /// <summary>
  33. /// 收件人,多个收件人用英文逗号隔开
  34. /// </summary>
  35. public string Tos { get; set; }
  36. /// <summary>
  37. /// 是否启用SSL,默认已启用
  38. /// </summary>
  39. public bool EnableSsl { get; set; } = true;
  40. /// <summary>
  41. /// 邮件消息对象
  42. /// </summary>
  43. MailMessage GetClient
  44. {
  45. get
  46. {
  47. if (string.IsNullOrEmpty(Tos)) return null;
  48. MailMessage mailMessage = new MailMessage();
  49. //多个接收者
  50. foreach (string _str in Tos.Split(','))
  51. {
  52. mailMessage.To.Add(_str);
  53. }
  54. mailMessage.From = new MailAddress(Username, Username);
  55. mailMessage.Subject = Subject;
  56. mailMessage.Body = Body;
  57. mailMessage.IsBodyHtml = true;
  58. mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  59. mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
  60. mailMessage.Priority = MailPriority.High;
  61. return mailMessage;
  62. }
  63. }
  64. SmtpClient GetSmtpClient => new SmtpClient
  65. {
  66. UseDefaultCredentials = false,
  67. EnableSsl = EnableSsl,
  68. Host = SmtpServer,
  69. Port = SmtpPort,
  70. Credentials = new System.Net.NetworkCredential(Username, Password),
  71. DeliveryMethod = SmtpDeliveryMethod.Network,
  72. };
  73. //回调方法
  74. Action<string> actionSendCompletedCallback = null;
  75. /// <summary>
  76. /// 使用异步发送邮件
  77. /// </summary>
  78. /// <param name="completedCallback">邮件发送后的回调方法</param>
  79. /// <returns></returns>
  80. public void SendAsync(Action<string> completedCallback)
  81. {
  82. using (SmtpClient smtpClient = GetSmtpClient)
  83. {
  84. using (MailMessage mailMessage = GetClient)
  85. {
  86. if (smtpClient == null || mailMessage == null) return;
  87. Subject = Subject;
  88. Body = Body;
  89. //EnableSsl = false;
  90. //发送邮件回调方法
  91. actionSendCompletedCallback = completedCallback;
  92. smtpClient.SendCompleted += SendCompletedCallback;
  93. smtpClient.SendAsync(mailMessage, "true"); //异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// 使用同步发送邮件
  99. /// </summary>
  100. public void Send()
  101. {
  102. using (SmtpClient smtpClient = GetSmtpClient)
  103. {
  104. using (MailMessage mailMessage = GetClient)
  105. {
  106. if (smtpClient == null || mailMessage == null) return;
  107. Subject = Subject;
  108. Body = Body;
  109. //EnableSsl = false;
  110. smtpClient.Send(mailMessage); //异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 异步操作完成后执行回调方法
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  120. {
  121. //同一组件下不需要回调方法,直接在此写入日志即可
  122. //写入日志
  123. //return;
  124. if (actionSendCompletedCallback == null) return;
  125. string message = string.Empty;
  126. if (e.Cancelled)
  127. {
  128. message = "异步操作取消";
  129. }
  130. else if (e.Error != null)
  131. {
  132. message = (string.Format("UserState:{0},Message:{1}", (string)e.UserState, e.Error.ToString()));
  133. }
  134. else
  135. message = (string)e.UserState;
  136. //执行回调方法
  137. actionSendCompletedCallback(message);
  138. }
  139. }
  140. #pragma warning restore 1591
  141. }