MailHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MailKit.Net.Smtp;
  6. using System.Configuration;
  7. using Bowin.Common.Utility;
  8. using System.IO;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.Configuration;
  11. using MimeKit;
  12. using MailKit.Net.Pop3;
  13. namespace Bowin.Common.Mail
  14. {
  15. public static class MailHelper
  16. {
  17. private const int _DEFAULT_SMTP_PORT = 25;
  18. private const int _DEFAULT_POP3_PORT = 110;
  19. private static MailSettings Settings { get; set; }
  20. static MailHelper()
  21. {
  22. var configuration = new ConfigurationBuilder()
  23. .SetBasePath(Directory.GetCurrentDirectory())
  24. .AddJsonFile("appsettings.json")
  25. .Build();
  26. Settings = configuration.GetSection("BowinMail").Get<MailSettings>();
  27. }
  28. private static string GetSenderEmail()
  29. {
  30. string hostName = Settings.SMTPServer.Substring(Settings.SMTPServer.IndexOf('.') + 1);
  31. return Settings.UserName + "@" + hostName;
  32. }
  33. public static void SendMail(string receiver, string title, string content)
  34. {
  35. if (string.IsNullOrEmpty(Settings.SMTPServer))
  36. {
  37. throw new SendMailException("请指定发送邮件的STMP服务器地址。");
  38. }
  39. if (!Settings.SMTPPort.HasValue)
  40. {
  41. Settings.SMTPPort = _DEFAULT_SMTP_PORT;
  42. }
  43. if (!Settings.SMTPIsSSL.HasValue)
  44. {
  45. Settings.SMTPIsSSL = false;
  46. }
  47. if (string.IsNullOrEmpty(Settings.UserName) || string.IsNullOrEmpty(Settings.Password))
  48. {
  49. throw new SendMailException("请指定发送邮件的账号和密码。");
  50. }
  51. MimeMessage message = new MimeMessage();
  52. message.From.Add(new MailboxAddress(GetSenderEmail()));
  53. message.To.Add(new MailboxAddress(receiver));
  54. message.Subject = title;
  55. message.Body = new BodyBuilder()
  56. {
  57. HtmlBody = content
  58. }.ToMessageBody();
  59. using (var client = new SmtpClient())
  60. {
  61. try
  62. {
  63. client.ServerCertificateValidationCallback = (s, c, h, e) => true;
  64. client.Connect(Settings.SMTPServer, Settings.SMTPPort.Value, Settings.SMTPIsSSL.Value);
  65. client.AuthenticationMechanisms.Remove("XOAUTH2");
  66. client.Authenticate(Settings.UserName, Settings.Password);
  67. client.Send(message);
  68. client.Disconnect(true);
  69. }
  70. catch (Exception ex)
  71. {
  72. throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
  73. }
  74. }
  75. }
  76. public static List<MimeMessage> ReceiveMail(string mailAddress)
  77. {
  78. if (string.IsNullOrEmpty(Settings.Pop3Server))
  79. {
  80. throw new ReceiveMailException("请指定发送邮件的POP3服务器地址。");
  81. }
  82. if (!Settings.Pop3Port.HasValue)
  83. {
  84. Settings.Pop3Port = _DEFAULT_POP3_PORT;
  85. }
  86. if (!Settings.Pop3IsSSL.HasValue)
  87. {
  88. Settings.Pop3IsSSL = false;
  89. }
  90. if (string.IsNullOrEmpty(Settings.UserName) || string.IsNullOrEmpty(Settings.Password))
  91. {
  92. throw new ReceiveMailException("请指定接收邮件的账号和密码。");
  93. }
  94. List<MimeMessage> mailList = new List<MimeMessage>();
  95. using (var client = new Pop3Client())
  96. {
  97. client.ServerCertificateValidationCallback = (s, c, h, e) => true;
  98. client.Connect(Settings.Pop3Server, Settings.Pop3Port.Value, Settings.Pop3IsSSL.Value);
  99. client.AuthenticationMechanisms.Remove("XOAUTH2");
  100. client.Authenticate(Settings.UserName, Settings.Password);
  101. for (int i = 0; i < client.Count; i++)
  102. {
  103. var message = client.GetMessage(i);
  104. mailList.Add(message);
  105. }
  106. client.Disconnect(true);
  107. }
  108. return mailList;
  109. }
  110. }
  111. }