EmailHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.IO;
  2. using System;
  3. using System.Net.Mail;
  4. using System.Text;
  5. namespace ProductionLineMonitor.Core.Utils
  6. {
  7. public class EmailHelper
  8. {
  9. public enum MailType
  10. {
  11. Warning,
  12. Message
  13. }
  14. public class EmailInfo
  15. {
  16. public string Topic { get; set; } = string.Empty;
  17. public string ReceiverAppSettingName { get; set; } = string.Empty;
  18. }
  19. public static void Send(string recipient, string subject, string body)
  20. {
  21. using var message = new MailMessage();
  22. message.From = new MailAddress("EQP.line@eink.com");
  23. message.To.Add(recipient);
  24. message.Subject = subject;
  25. message.SubjectEncoding = Encoding.UTF8;
  26. message.Body =
  27. "<html><head><style>body { font-family: 宋体, sans-serif; font-size: 12px; }</style></head><body>" +
  28. body + "</body></html>";
  29. message.BodyEncoding = Encoding.UTF8;
  30. message.IsBodyHtml = true;
  31. using var smtpClient = new SmtpClient("mse.toc.eink.com", 25);
  32. smtpClient.Send(message);
  33. }
  34. /// <summary>
  35. /// 发送邮件
  36. /// </summary>
  37. /// <param name="mailtype">邮件类型</param>
  38. /// <param name="ReceiverAppSettingName">收件人配置名</param>
  39. /// <param name="CCAppSettingName">抄送人配置名</param>
  40. /// <param name="BCCAppSettingName">密送人配置名</param>
  41. /// <param name="SenderAppSettingName">发送人配置名</param>
  42. /// <param name="MailSenderName">发送人名</param>
  43. /// <param name="MailSubject">邮件标题</param>
  44. /// <param name="MailBody">邮件正文</param>
  45. /// <param name="AttachmentFilePath">邮件附件绝对路径地址</param>
  46. /// <param name="encoding">邮件编码格式</param>
  47. /// <returns></returns>
  48. public static void SendMail(MailType mailtype, string ReceiverAppSettingName, string CCAppSettingName, string BCCAppSettingName,
  49. string SenderAppSettingName, string MailSenderName, string MailSubject, string MailBody, string[] AttachmentFilePath, Encoding encoding)
  50. {
  51. MailMessage msg = new MailMessage();
  52. if (string.IsNullOrEmpty(ReceiverAppSettingName))
  53. {
  54. throw new Exception("至少需要一组收件人!");
  55. }
  56. string[] Receiver = ReceiverAppSettingName.Split(';');
  57. for (int i = 0; i < Receiver.Length; i++)
  58. {
  59. if (!string.IsNullOrEmpty(Receiver[i]))
  60. {
  61. msg.To.Add(Receiver[i]);
  62. }
  63. }
  64. if (mailtype == MailType.Warning)
  65. {
  66. // 重要性
  67. msg.Priority = MailPriority.High;
  68. }
  69. // 附件
  70. if (AttachmentFilePath != null && AttachmentFilePath.Length > 0)
  71. {
  72. for (int i = 0; i < AttachmentFilePath.Length; i++)
  73. {
  74. if (File.Exists(AttachmentFilePath[i]))
  75. {
  76. msg.Attachments.Add(new Attachment(new FileStream(AttachmentFilePath[i], FileMode.Open), Path.GetFileName(AttachmentFilePath[i])));
  77. }
  78. }
  79. }
  80. // 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码
  81. if (string.IsNullOrEmpty(SenderAppSettingName))
  82. {
  83. throw new Exception("发件人地址不可以为空!");
  84. }
  85. msg.From = new MailAddress(SenderAppSettingName, MailSenderName, encoding);
  86. // 邮件标题
  87. msg.Subject = MailSubject;
  88. // 邮件标题编码
  89. msg.SubjectEncoding = encoding;
  90. // 邮件内容
  91. msg.Body = MailBody;
  92. // 邮件内容编码
  93. msg.BodyEncoding = encoding;
  94. // 是否是HTML邮件
  95. msg.IsBodyHtml = true;
  96. // 客户端
  97. SmtpClient client = new SmtpClient("mse.toc.eink.com", 25);
  98. try
  99. {
  100. // 发送
  101. client.Send(msg);
  102. }
  103. catch (Exception ex)
  104. {
  105. throw ex;
  106. }
  107. }
  108. }
  109. }