EmailHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Mail;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Xml.Linq;
  12. namespace FCS.Common
  13. {
  14. public class EmailHelper
  15. {
  16. private static Email model;
  17. static EmailHelper()
  18. {
  19. #region 获取配置数据
  20. string configFile = AppDomain.CurrentDomain.BaseDirectory + "/XmlConfig/Email.xml";
  21. model = CommonHelper.ConvertXMLToObject<Email>(configFile, "UrlSetting")[0] as Email;
  22. #endregion
  23. }
  24. /// <summary>
  25. /// 发送邮件到指定收件人
  26. /// </summary>
  27. /// <param name="to">收件人地址</param>
  28. /// <param name="subject">主题</param>
  29. /// <param name="mailBody">正文内容(支持HTML)</param>
  30. /// <param name="copyTos">抄送地址列表</param>
  31. /// <returns>是否发送成功</returns>
  32. public static bool Send(string[] to, string subject, string mailBody, params string[] copyTos)
  33. {
  34. return Send(to, subject, mailBody, copyTos, new string[] { }, MailPriority.Normal);
  35. }
  36. /// <summary>
  37. /// 默认发送
  38. /// </summary>
  39. /// <param name="subject"></param>
  40. /// <param name="mailBody"></param>
  41. /// <returns></returns>
  42. public static bool Send(string subject, string mailBody)
  43. {
  44. return Send(ConfigurationManager.AppSettings["MailTo"].Split(','), subject, mailBody, new string[] { }, new string[] { }, MailPriority.Normal);
  45. }
  46. /// <summary>
  47. /// 发送邮件到指定收件人
  48. /// </summary>
  49. /// <remarks>
  50. /// </remarks>
  51. /// <param name="tos">收件人地址列表</param>
  52. /// <param name="subject">主题</param>
  53. /// <param name="mailBody">正文内容(支持HTML)</param>
  54. /// <param name="ccs">抄送地址列表</param>
  55. /// <param name="bccs">密件抄送地址列表</param>
  56. /// <param name="priority">此邮件的优先级</param>
  57. /// <param name="attachments">附件列表</param>
  58. /// <returns>是否发送成功</returns>
  59. /// <exception cref="System.ArgumentNullException">attachments</exception>
  60. public static bool Send(string[] tos, string subject, string mailBody, string[] ccs, string[] bccs, MailPriority priority, params Attachment[] attachments)
  61. {
  62. try
  63. {
  64. Encoding encode = Encoding.UTF8;
  65. SmtpClient smtp = new SmtpClient();
  66. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//将smtp的出站方式设为 Network
  67. smtp.Host = model.SmtpServer;//指定 smtp 服务器地址
  68. smtp.Port = model.SmtpServerPort;//指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
  69. //如果需要认证,则用此的方式
  70. smtp.EnableSsl = model.SmtpEnableSsl;//smtp服务器是否启用SSL加密
  71. //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
  72. smtp.UseDefaultCredentials = true;
  73. smtp.Credentials = new NetworkCredential(model.SmtpUsername, model.SmtpUserPassword);
  74. MailMessage mail = new MailMessage();
  75. mail.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
  76. MailAddress ma = new MailAddress(model.SmtpUsername, model.SmtpDisplayName, encode);
  77. //收件方看到的邮件来源;
  78. //第一个参数是发信人邮件地址
  79. //第二参数是发信人显示的名称
  80. //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码
  81. //936是简体中文的codepage值
  82. //注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败
  83. mail.From = ma;
  84. //邮件的接收者,支持群发,多个地址之间用(半角逗号)分开
  85. foreach (var acceptMail in tos)
  86. {
  87. mail.To.Add(acceptMail);
  88. }
  89. if (ccs != null)
  90. foreach (var acceptMail in ccs)
  91. {
  92. mail.Bcc.Add(acceptMail);
  93. }
  94. mail.SubjectEncoding = encode;
  95. mail.Subject = subject;
  96. mail.IsBodyHtml = true;//是否启用html代码
  97. mail.BodyEncoding = encode;
  98. mail.Body = mailBody;
  99. smtp.Send(mail);
  100. Trace.WriteLine("发送成功!");
  101. return true;
  102. }
  103. catch (Exception ex)
  104. {
  105. Thread.CurrentThread.Abort();
  106. Trace.WriteLine("发送失败!");
  107. return false;
  108. }
  109. }
  110. }
  111. public class Email
  112. {
  113. public string SmtpServer { get; set; }//SMTP服务器
  114. public int SmtpServerPort { get; set; }//SMTP端口
  115. public bool SmtpEnableSsl { get; set; }//是否SSL加密连接
  116. public string SmtpUsername { get; set; }//发件人邮箱地址
  117. public string SmtpDisplayName { get; set; }//发件人昵称
  118. public string SmtpUserPassword { get; set; }//授权码
  119. }
  120. }