123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System.IO;
- using System;
- using System.Net.Mail;
- using System.Text;
- namespace ProductionLineMonitor.Core.Utils
- {
- public class EmailHelper
- {
- public enum MailType
- {
- Warning,
- Message
- }
- public class EmailInfo
- {
- public string Topic { get; set; } = string.Empty;
- public string ReceiverAppSettingName { get; set; } = string.Empty;
- }
- public static void Send(string recipient, string subject, string body)
- {
- using var message = new MailMessage();
- message.From = new MailAddress("EQP.line@eink.com");
- message.To.Add(recipient);
- message.Subject = subject;
- message.SubjectEncoding = Encoding.UTF8;
- message.Body =
- "<html><head><style>body { font-family: 宋体, sans-serif; font-size: 12px; }</style></head><body>" +
- body + "</body></html>";
- message.BodyEncoding = Encoding.UTF8;
- message.IsBodyHtml = true;
- using var smtpClient = new SmtpClient("mse.toc.eink.com", 25);
- smtpClient.Send(message);
- }
- /// <summary>
- /// 发送邮件
- /// </summary>
- /// <param name="mailtype">邮件类型</param>
- /// <param name="ReceiverAppSettingName">收件人配置名</param>
- /// <param name="CCAppSettingName">抄送人配置名</param>
- /// <param name="BCCAppSettingName">密送人配置名</param>
- /// <param name="SenderAppSettingName">发送人配置名</param>
- /// <param name="MailSenderName">发送人名</param>
- /// <param name="MailSubject">邮件标题</param>
- /// <param name="MailBody">邮件正文</param>
- /// <param name="AttachmentFilePath">邮件附件绝对路径地址</param>
- /// <param name="encoding">邮件编码格式</param>
- /// <returns></returns>
- public static void SendMail(MailType mailtype, string ReceiverAppSettingName, string CCAppSettingName, string BCCAppSettingName,
- string SenderAppSettingName, string MailSenderName, string MailSubject, string MailBody, string[] AttachmentFilePath, Encoding encoding)
- {
- MailMessage msg = new MailMessage();
- if (string.IsNullOrEmpty(ReceiverAppSettingName))
- {
- throw new Exception("至少需要一组收件人!");
- }
- string[] Receiver = ReceiverAppSettingName.Split(';');
- for (int i = 0; i < Receiver.Length; i++)
- {
- if (!string.IsNullOrEmpty(Receiver[i]))
- {
- msg.To.Add(Receiver[i]);
- }
- }
- if (mailtype == MailType.Warning)
- {
- // 重要性
- msg.Priority = MailPriority.High;
- }
- // 附件
- if (AttachmentFilePath != null && AttachmentFilePath.Length > 0)
- {
- for (int i = 0; i < AttachmentFilePath.Length; i++)
- {
- if (File.Exists(AttachmentFilePath[i]))
- {
- msg.Attachments.Add(new Attachment(new FileStream(AttachmentFilePath[i], FileMode.Open), Path.GetFileName(AttachmentFilePath[i])));
- }
- }
- }
- // 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码
- if (string.IsNullOrEmpty(SenderAppSettingName))
- {
- throw new Exception("发件人地址不可以为空!");
- }
- msg.From = new MailAddress(SenderAppSettingName, MailSenderName, encoding);
- // 邮件标题
- msg.Subject = MailSubject;
- // 邮件标题编码
- msg.SubjectEncoding = encoding;
- // 邮件内容
- msg.Body = MailBody;
- // 邮件内容编码
- msg.BodyEncoding = encoding;
- // 是否是HTML邮件
- msg.IsBodyHtml = true;
- // 客户端
- SmtpClient client = new SmtpClient("mse.toc.eink.com", 25);
- try
- {
- // 发送
- client.Send(msg);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
|