纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.1 KiB

3 years ago
  1. using System;
  2. using System.Net;
  3. using System.Net.Mail;
  4. using System.Text;
  5. using System.Threading;
  6. namespace NFine.Code
  7. {
  8. public class MailHelper
  9. {
  10. /// <summary>
  11. /// 邮件服务器地址
  12. /// </summary>
  13. public string MailServer { get; set; }
  14. /// <summary>
  15. /// 用户名
  16. /// </summary>
  17. public string MailUserName { get; set; }
  18. /// <summary>
  19. /// 密码
  20. /// </summary>
  21. public string MailPassword { get; set; }
  22. /// <summary>
  23. /// 名称
  24. /// </summary>
  25. public string MailName { get; set; }
  26. /// <summary>
  27. /// 同步发送邮件
  28. /// </summary>
  29. /// <param name="to">收件人邮箱地址</param>
  30. /// <param name="subject">主题</param>
  31. /// <param name="body">内容</param>
  32. /// <param name="encoding">编码</param>
  33. /// <param name="isBodyHtml">是否Html</param>
  34. /// <param name="enableSsl">是否SSL加密连接</param>
  35. /// <returns>是否成功</returns>
  36. public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
  37. {
  38. try
  39. {
  40. MailMessage message = new MailMessage();
  41. // 接收人邮箱地址
  42. message.To.Add(new MailAddress(to));
  43. message.From = new MailAddress(MailUserName, MailName);
  44. message.BodyEncoding = Encoding.GetEncoding(encoding);
  45. message.Body = body;
  46. //GB2312
  47. message.SubjectEncoding = Encoding.GetEncoding(encoding);
  48. message.Subject = subject;
  49. message.IsBodyHtml = isBodyHtml;
  50. SmtpClient smtpclient = new SmtpClient(MailServer, 25);
  51. smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
  52. //SSL连接
  53. smtpclient.EnableSsl = enableSsl;
  54. smtpclient.Send(message);
  55. return true;
  56. }
  57. catch (Exception)
  58. {
  59. throw;
  60. }
  61. }
  62. /// <summary>
  63. /// 异步发送邮件 独立线程
  64. /// </summary>
  65. /// <param name="to">邮件接收人</param>
  66. /// <param name="title">邮件标题</param>
  67. /// <param name="body">邮件内容</param>
  68. /// <param name="port">端口号</param>
  69. /// <returns></returns>
  70. public void SendByThread(string to, string title, string body, int port = 25)
  71. {
  72. new Thread(new ThreadStart(delegate()
  73. {
  74. try
  75. {
  76. SmtpClient smtp = new SmtpClient();
  77. //邮箱的smtp地址
  78. smtp.Host = MailServer;
  79. //端口号
  80. smtp.Port = port;
  81. //构建发件人的身份凭据类
  82. smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
  83. //构建消息类
  84. MailMessage objMailMessage = new MailMessage();
  85. //设置优先级
  86. objMailMessage.Priority = MailPriority.High;
  87. //消息发送人
  88. objMailMessage.From = new MailAddress(MailUserName, MailName, System.Text.Encoding.UTF8);
  89. //收件人
  90. objMailMessage.To.Add(to);
  91. //标题
  92. objMailMessage.Subject = title.Trim();
  93. //标题字符编码
  94. objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
  95. //正文
  96. objMailMessage.Body = body.Trim();
  97. objMailMessage.IsBodyHtml = true;
  98. //内容字符编码
  99. objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  100. //发送
  101. smtp.Send(objMailMessage);
  102. }
  103. catch (Exception)
  104. {
  105. throw;
  106. }
  107. })).Start();
  108. }
  109. }
  110. }