MailHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using Infrastructure;
  2. using MailKit.Net.Smtp;
  3. using MimeKit;
  4. using MimeKit.Text;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. namespace ZR.Common
  9. {
  10. public class MailHelper
  11. {
  12. /// <summary>
  13. /// 发送人邮箱
  14. /// </summary>
  15. public string FromEmail { get; set; } = "";
  16. /// <summary>
  17. /// 发送人密码
  18. /// </summary>
  19. public string FromPwd { get; set; } = "";
  20. /// <summary>
  21. /// 发送协议
  22. /// </summary>
  23. public string Smtp { get; set; } = "smtp.qq.com";
  24. /// <summary>
  25. /// 协议端口
  26. /// </summary>
  27. public int Port { get; set; } = 587;
  28. /// <summary>
  29. /// 邮件签名
  30. /// </summary>
  31. public string Signature { get; set; }
  32. /// <summary>
  33. /// 是否使用SSL协议
  34. /// </summary>
  35. public bool UseSsl { get; set; } = false;
  36. private readonly MailOptions mailOptions = new();
  37. public MailHelper()
  38. {
  39. AppSettings.Bind("MailOptions", mailOptions);
  40. FromEmail = mailOptions.From;
  41. Smtp = mailOptions.Smtp;
  42. FromPwd = mailOptions.Password;
  43. Port = mailOptions.Port;
  44. }
  45. public MailHelper(string fromEmail, string smtp, int port, string fromPwd)
  46. {
  47. FromEmail = fromEmail;
  48. Smtp = smtp;
  49. FromPwd = fromPwd;
  50. Port = port;
  51. }
  52. public MailHelper(string fromEmail, string fromPwd)
  53. {
  54. FromEmail = fromEmail;
  55. FromPwd = fromPwd;
  56. }
  57. /// <summary>
  58. /// 发送一个
  59. /// </summary>
  60. /// <param name="toAddress"></param>
  61. /// <param name="subject"></param>
  62. /// <param name="text"></param>
  63. /// <param name="path"></param>
  64. public void SendMail(string toAddress, string subject, string text, string path = "", string html = "")
  65. {
  66. IEnumerable<MailboxAddress> mailboxes = new List<MailboxAddress>() {
  67. new MailboxAddress(toAddress, toAddress)
  68. };
  69. SendMail(mailboxes, subject, text, path, html);
  70. }
  71. /// <summary>
  72. /// 发送多个邮箱
  73. /// </summary>
  74. /// <param name="toAddress"></param>
  75. /// <param name="subject"></param>
  76. /// <param name="text"></param>
  77. /// <param name="path"></param>
  78. public void SendMail(string[] toAddress, string subject, string text, string path = "", string html = "")
  79. {
  80. IList<MailboxAddress> mailboxes = new List<MailboxAddress>() { };
  81. foreach (var item in toAddress)
  82. {
  83. mailboxes.Add(new MailboxAddress(item, item));
  84. }
  85. SendMail(mailboxes, subject, text, path, html);
  86. }
  87. /// <summary>
  88. /// 发送邮件
  89. /// </summary>
  90. /// <param name="toAddress"></param>
  91. /// <param name="subject">主题</param>
  92. /// <param name="text"></param>
  93. /// <param name="path">附件url地址</param>
  94. /// <param name="html">网页HTML内容</param>
  95. private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
  96. {
  97. MimeMessage message = new MimeMessage();
  98. //发件人
  99. message.From.Add(new MailboxAddress(FromEmail, FromEmail));
  100. //收件人
  101. message.To.AddRange(toAddress);
  102. message.Subject = subject;
  103. message.Date = DateTime.Now;
  104. //创建附件Multipart
  105. Multipart multipart = new Multipart("mixed");
  106. var alternative = new MultipartAlternative();
  107. //html内容
  108. if (!string.IsNullOrEmpty(html))
  109. {
  110. var Html = new TextPart(TextFormat.Html)
  111. {
  112. Text = html
  113. };
  114. alternative.Add(Html);
  115. }
  116. //文本内容
  117. //if (!string.IsNullOrEmpty(text))
  118. //{
  119. var plain = new TextPart(TextFormat.Plain)
  120. {
  121. Text = text + "\r\n\n\n" + mailOptions.Signature
  122. };
  123. alternative.Add(plain);
  124. //}
  125. //附件
  126. if (!string.IsNullOrEmpty(path))
  127. {
  128. string[] files = path.Split(",");
  129. foreach (var file in files)
  130. {
  131. MimePart attachment = new()
  132. {
  133. Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),
  134. //读取文件,只能用绝对路径
  135. ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
  136. ContentTransferEncoding = ContentEncoding.Base64,
  137. //文件名字
  138. FileName = Path.GetFileName(path)
  139. };
  140. alternative.Add(attachment);
  141. }
  142. }
  143. multipart.Add(alternative);
  144. //赋值邮件内容
  145. message.Body = multipart;
  146. //开始发送
  147. using var client = new SmtpClient();
  148. client.ServerCertificateValidationCallback = (s, c, h, e) => true;
  149. //Smtp服务器
  150. //client.Connect("smtp.qq.com", 587, false);
  151. client.Connect(Smtp, Port, true);
  152. //登录,发送
  153. //特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
  154. client.Authenticate(FromEmail, FromPwd);
  155. client.Send(message);
  156. //断开
  157. client.Disconnect(true);
  158. Console.WriteLine($"发送邮件成功{DateTime.Now}");
  159. }
  160. }
  161. }