宁虹看板
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.

157 lines
5.9 KiB

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