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.

156 lines
5.8 KiB

  1. 
  2. using System;
  3. using System.Net;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using System.Threading;
  7. namespace NFine.Code
  8. {
  9. public class MailHelper
  10. {
  11. /// <summary>
  12. /// 邮件服务器地址
  13. /// </summary>
  14. public string MailServer { get; set; }
  15. /// <summary>
  16. /// 用户名
  17. /// </summary>
  18. public string MailUserName { get; set; }
  19. /// <summary>
  20. /// 密码
  21. /// </summary>
  22. public string MailPassword { get; set; }
  23. /// <summary>
  24. /// 名称
  25. /// </summary>
  26. public string MailName { get; set; }
  27. /// <summary>
  28. /// 端口
  29. /// </summary>
  30. public int Post { get; set; }
  31. public MailHelper(string _MailServer,string _MailUserName,string _MailPassword,string _MailName,int _Post)
  32. {
  33. MailServer = _MailServer;
  34. MailUserName = _MailUserName;
  35. MailPassword = _MailPassword;
  36. MailName = _MailName;
  37. Post = _Post;
  38. }
  39. /// <summary>
  40. /// 同步发送邮件
  41. /// </summary>
  42. /// <param name="to">收件人邮箱地址</param>
  43. /// <param name="subject">主题</param>
  44. /// <param name="body">内容</param>
  45. /// <param name="encoding">编码</param>
  46. /// <param name="isBodyHtml">是否Html</param>
  47. /// <param name="enableSsl">是否SSL加密连接</param>
  48. /// <returns>是否成功</returns>
  49. public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
  50. {
  51. try
  52. {
  53. MailMessage message = new MailMessage();
  54. // 接收人邮箱地址
  55. if (!string.IsNullOrEmpty(to))
  56. {
  57. string[] TOList = to.Split(',');
  58. foreach(string item in TOList)
  59. {
  60. message.To.Add(new MailAddress(item));
  61. }
  62. }
  63. message.From = new MailAddress(MailUserName, MailName);
  64. message.BodyEncoding = Encoding.GetEncoding(encoding);
  65. message.Body = body;
  66. //GB2312
  67. message.SubjectEncoding = Encoding.GetEncoding(encoding);
  68. message.Subject = subject;
  69. message.IsBodyHtml = isBodyHtml;
  70. SmtpClient smtpclient = new SmtpClient(MailServer, Post);
  71. smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
  72. //SSL连接
  73. smtpclient.EnableSsl = enableSsl;
  74. smtpclient.Send(message);
  75. LogEntity logEntity = new LogEntity();
  76. logEntity.F_ModuleName = "发邮件";
  77. logEntity.F_Type = "SendMail";
  78. var LoginInfo = OperatorProvider.Provider.GetCurrent();
  79. logEntity.F_Account = LoginInfo.UserCode;
  80. logEntity.F_NickName = LoginInfo.UserName;
  81. logEntity.F_Result = false;
  82. logEntity.F_Description = "发邮件成功";
  83. new LogApp().WriteDbLog(logEntity);
  84. return true;
  85. }
  86. catch (Exception ex)
  87. {
  88. LogEntity logEntity = new LogEntity();
  89. logEntity.F_ModuleName = "发邮件";
  90. logEntity.F_Type = "SendMail";
  91. var LoginInfo = OperatorProvider.Provider.GetCurrent();
  92. logEntity.F_Account = LoginInfo.UserCode;
  93. logEntity.F_NickName = LoginInfo.UserName;
  94. logEntity.F_Result = false;
  95. logEntity.F_Description = "发邮件失败:"+ex.Message;
  96. new LogApp().WriteDbLog(logEntity);
  97. throw;
  98. }
  99. }
  100. /// <summary>
  101. /// 异步发送邮件 独立线程
  102. /// </summary>
  103. /// <param name="to">邮件接收人</param>
  104. /// <param name="title">邮件标题</param>
  105. /// <param name="body">邮件内容</param>
  106. /// <param name="port">端口号</param>
  107. /// <returns></returns>
  108. public void SendByThread(string to, string title, string body, int port = 25)
  109. {
  110. new Thread(new ThreadStart(delegate()
  111. {
  112. try
  113. {
  114. SmtpClient smtp = new SmtpClient();
  115. //邮箱的smtp地址
  116. smtp.Host = MailServer;
  117. //端口号
  118. smtp.Port = port;
  119. //构建发件人的身份凭据类
  120. smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
  121. //构建消息类
  122. MailMessage objMailMessage = new MailMessage();
  123. //设置优先级
  124. objMailMessage.Priority = MailPriority.High;
  125. //消息发送人
  126. objMailMessage.From = new MailAddress(MailUserName, MailName, System.Text.Encoding.UTF8);
  127. //收件人
  128. objMailMessage.To.Add(to);
  129. //标题
  130. objMailMessage.Subject = title.Trim();
  131. //标题字符编码
  132. objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
  133. //正文
  134. objMailMessage.Body = body.Trim();
  135. objMailMessage.IsBodyHtml = true;
  136. //内容字符编码
  137. objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
  138. //发送
  139. smtp.Send(objMailMessage);
  140. }
  141. catch (Exception)
  142. {
  143. throw;
  144. }
  145. })).Start();
  146. }
  147. }
  148. }