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 =
"
" +
body + "";
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
using var smtpClient = new SmtpClient("mse.toc.eink.com", 25);
smtpClient.Send(message);
}
///
/// 发送邮件
///
/// 邮件类型
/// 收件人配置名
/// 抄送人配置名
/// 密送人配置名
/// 发送人配置名
/// 发送人名
/// 邮件标题
/// 邮件正文
/// 邮件附件绝对路径地址
/// 邮件编码格式
///
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;
}
}
}
}