EmailHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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="AttachmentFileName">邮件附件命名</param>
  47. /// <param name="encoding">邮件编码格式</param>
  48. /// <returns></returns>
  49. public static void SendMail(MailType mailtype, string ReceiverAppSettingName, string CCAppSettingName, string BCCAppSettingName,
  50. string SenderAppSettingName, string MailSenderName, string MailSubject, string MailBody, string[] AttachmentFilePath, string[] AttachmentFileName, Encoding encoding)
  51. {
  52. MailMessage msg = new MailMessage();
  53. if (string.IsNullOrEmpty(ReceiverAppSettingName))
  54. {
  55. throw new Exception("至少需要一组收件人!");
  56. }
  57. string[] Receiver = ReceiverAppSettingName.Split(';');
  58. for (int i = 0; i < Receiver.Length; i++)
  59. {
  60. if (!string.IsNullOrEmpty(Receiver[i]))
  61. {
  62. msg.To.Add(Receiver[i]);
  63. }
  64. }
  65. if (mailtype == MailType.Warning)
  66. {
  67. // 重要性
  68. msg.Priority = MailPriority.High;
  69. }
  70. // 附件
  71. if (AttachmentFilePath != null && AttachmentFilePath.Length > 0)
  72. {
  73. for (int i = 0; i < AttachmentFilePath.Length; i++)
  74. {
  75. if (File.Exists(AttachmentFilePath[i]))
  76. {
  77. msg.Attachments.Add(new Attachment(new FileStream(AttachmentFilePath[i], FileMode.Open), $"{AttachmentFileName[i]}.xlsx"));
  78. }
  79. }
  80. }
  81. // 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码
  82. if (string.IsNullOrEmpty(SenderAppSettingName))
  83. {
  84. throw new Exception("发件人地址不可以为空!");
  85. }
  86. msg.From = new MailAddress(SenderAppSettingName, MailSenderName, encoding);
  87. // 邮件标题
  88. msg.Subject = MailSubject;
  89. // 邮件标题编码
  90. msg.SubjectEncoding = encoding;
  91. // 邮件内容
  92. msg.Body = MailBody;
  93. // 邮件内容编码
  94. msg.BodyEncoding = encoding;
  95. // 是否是HTML邮件
  96. msg.IsBodyHtml = true;
  97. // 客户端
  98. SmtpClient client = new SmtpClient("mse.toc.eink.com", 25);
  99. try
  100. {
  101. // 发送
  102. client.Send(msg);
  103. msg.Attachments.Dispose();
  104. }
  105. catch (Exception ex)
  106. {
  107. throw ex;
  108. }
  109. }
  110. }
  111. }