EmailHelper.cs 5.1 KB

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