Email.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System.ComponentModel;
  2. using System.Net.Mail;
  3. using System.Net;
  4. using System.Text;
  5. using Masuit.Tools.Models;
  6. using Masuit.Tools;
  7. namespace EasyTemplate.Tool;
  8. public class Email : Disposable
  9. {
  10. private Action<string> _actionSendCompletedCallback;
  11. //
  12. // 摘要:
  13. // 发件人用户名
  14. public EmailAddress Username { get; set; }
  15. //
  16. // 摘要:
  17. // 发件人邮箱密码
  18. public string Password { get; set; }
  19. //
  20. // 摘要:
  21. // 发送服务器端口号,默认25
  22. public int SmtpPort { get; set; } = 25;
  23. //
  24. // 摘要:
  25. // 发送服务器地址
  26. public string SmtpServer { get; set; }
  27. //
  28. // 摘要:
  29. // 邮件标题
  30. public string Subject { get; set; }
  31. //
  32. // 摘要:
  33. // 邮件正文
  34. public string Body { get; set; }
  35. //
  36. // 摘要:
  37. // 收件人,多个收件人用英文逗号隔开
  38. public string Tos { get; set; }
  39. public List<string> CC { get; set; } = new List<string>();
  40. public List<string> BCC { get; set; } = new List<string>();
  41. //
  42. // 摘要:
  43. // 是否启用SSL,默认已启用
  44. public bool EnableSsl { get; set; } = true;
  45. //
  46. // 摘要:
  47. // 附件
  48. public List<Attachment> Attachments { get; set; } = new List<Attachment>();
  49. private MailMessage MailMessage => GetClient();
  50. public Email()
  51. {
  52. SmtpServer = "";// SMTP服务器
  53. SmtpPort = 587;// SMTP服务器端口
  54. EnableSsl = true;//使用SSL
  55. Username = "";// 邮箱用户名
  56. Password = "";// 邮箱密码
  57. SmtpClient = new SmtpClient()
  58. {
  59. UseDefaultCredentials = false,
  60. EnableSsl = EnableSsl,
  61. Host = SmtpServer,
  62. Port = SmtpPort,
  63. Credentials = new NetworkCredential(Username, Password),
  64. DeliveryMethod = SmtpDeliveryMethod.Network
  65. };
  66. SmtpClient.SendCompleted += SendCompletedCallback;
  67. }
  68. private SmtpClient SmtpClient { get; set; }
  69. //
  70. // 摘要:
  71. // 邮件消息对象
  72. private MailMessage GetClient()
  73. {
  74. if (string.IsNullOrEmpty(Tos))
  75. {
  76. return null;
  77. }
  78. MailMessage mailMessage = new MailMessage();
  79. string[] array = Tos.Split(',');
  80. foreach (string addresses in array)
  81. {
  82. mailMessage.To.Add(addresses);
  83. }
  84. foreach (string item in CC)
  85. {
  86. mailMessage.CC.Add(item);
  87. }
  88. foreach (string item2 in BCC)
  89. {
  90. mailMessage.Bcc.Add(item2);
  91. }
  92. mailMessage.From = new MailAddress(Username, Username);
  93. mailMessage.Subject = Subject;
  94. mailMessage.Body = Body;
  95. mailMessage.IsBodyHtml = true;
  96. mailMessage.BodyEncoding = Encoding.UTF8;
  97. mailMessage.SubjectEncoding = Encoding.UTF8;
  98. mailMessage.Priority = MailPriority.High;
  99. foreach (Attachment item3 in Attachments.AsNotNull())
  100. {
  101. mailMessage.Attachments.Add(item3);
  102. }
  103. return mailMessage;
  104. }
  105. //
  106. // 摘要:
  107. // 使用异步发送邮件
  108. //
  109. // 参数:
  110. // completedCallback:
  111. // 邮件发送后的回调方法
  112. public void SendAsync(Action<string> completedCallback)
  113. {
  114. if (MailMessage != null)
  115. {
  116. _actionSendCompletedCallback = completedCallback;
  117. SmtpClient.SendAsync(MailMessage, "true");
  118. }
  119. }
  120. //
  121. // 摘要:
  122. // 使用同步发送邮件
  123. public void Send()
  124. {
  125. if (MailMessage != null)
  126. {
  127. SmtpClient.Send(MailMessage);
  128. Dispose(disposing: true);
  129. }
  130. }
  131. //
  132. // 摘要:
  133. // 异步操作完成后执行回调方法
  134. //
  135. // 参数:
  136. // sender:
  137. //
  138. // e:
  139. private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  140. {
  141. if (_actionSendCompletedCallback != null)
  142. {
  143. string obj = (e.Cancelled ? "异步操作取消" : ((e.Error == null) ? ((string)e.UserState) : $"UserState:{(string)e.UserState},Message:{e.Error}"));
  144. _actionSendCompletedCallback(obj);
  145. Dispose(disposing: true);
  146. }
  147. }
  148. //
  149. // 摘要:
  150. // 释放
  151. //
  152. // 参数:
  153. // disposing:
  154. public override void Dispose(bool disposing)
  155. {
  156. MailMessage?.Dispose();
  157. SmtpClient?.Dispose();
  158. Attachments.ForEach(delegate (Attachment a)
  159. {
  160. a.Dispose();
  161. });
  162. }
  163. }
  164. public abstract class Disposable : IDisposable
  165. {
  166. private bool isDisposed;
  167. //
  168. // 摘要:
  169. // 终结器
  170. ~Disposable()
  171. {
  172. Dispose(disposing: false);
  173. }
  174. public void Dispose()
  175. {
  176. if (!isDisposed)
  177. {
  178. Dispose(disposing: true);
  179. isDisposed = true;
  180. GC.Collect();
  181. }
  182. }
  183. //
  184. // 摘要:
  185. // 释放
  186. //
  187. // 参数:
  188. // disposing:
  189. public abstract void Dispose(bool disposing);
  190. }