Email.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 var smtpClient = GetSmtpClient;
  83. using var mailMessage = GetClient;
  84. if (smtpClient == null || mailMessage == null) return;
  85. Subject = Subject;
  86. Body = Body;
  87. //EnableSsl = false;
  88. //发送邮件回调方法
  89. actionSendCompletedCallback = completedCallback;
  90. smtpClient.SendCompleted += SendCompletedCallback;
  91. smtpClient.SendAsync(mailMessage, "true"); //异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
  92. }
  93. /// <summary>
  94. /// 使用同步发送邮件
  95. /// </summary>
  96. public void Send()
  97. {
  98. using SmtpClient smtpClient = GetSmtpClient;
  99. using MailMessage mailMessage = GetClient;
  100. if (smtpClient == null || mailMessage == null) return;
  101. Subject = Subject;
  102. Body = Body;
  103. //EnableSsl = false;
  104. smtpClient.Send(mailMessage); //异步发送邮件,如果回调方法中参数不为"true"则表示发送失败
  105. }
  106. /// <summary>
  107. /// 异步操作完成后执行回调方法
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void SendCompletedCallback(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
  112. {
  113. //同一组件下不需要回调方法,直接在此写入日志即可
  114. //写入日志
  115. //return;
  116. if (actionSendCompletedCallback == null) return;
  117. string message;
  118. if (e.Cancelled)
  119. {
  120. message = "异步操作取消";
  121. }
  122. else if (e.Error != null)
  123. {
  124. message = ($"UserState:{(string)e.UserState},Message:{e.Error}");
  125. }
  126. else
  127. {
  128. message = (string)e.UserState;
  129. }
  130. //执行回调方法
  131. actionSendCompletedCallback(message);
  132. }
  133. }
  134. #pragma warning restore 1591
  135. }