|
|
|
|
|
using System.Text;
|
|
|
using DS.Module.Core;
|
|
|
using DS.WMS.Core.Code.Entity;
|
|
|
using MailKit.Net.Smtp;
|
|
|
using MailKit.Security;
|
|
|
using MimeKit;
|
|
|
using NLog;
|
|
|
using Logger = NLog.Logger;
|
|
|
|
|
|
namespace DS.WMS.Core.Op.EDI
|
|
|
{
|
|
|
public static class MailSendHelper
|
|
|
{
|
|
|
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
|
|
|
/// <summary>
|
|
|
/// 使用用户邮箱配置发送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="acc"></param>
|
|
|
/// <param name="subject"></param>
|
|
|
/// <param name="body"></param>
|
|
|
/// <param name="sendTo"></param>
|
|
|
/// <param name="attachList">附件列表,key为附件名称,value为文件字节数组</param>
|
|
|
public static async Task<DataResult> SendMail(CodeUserEmail acc, string subject, string body, string sendTo, params KeyValuePair<string, byte[]>[] attachList)
|
|
|
{
|
|
|
SmtpClient client = null;
|
|
|
try
|
|
|
{
|
|
|
var message = new MimeMessage();
|
|
|
|
|
|
if (!string.IsNullOrEmpty(acc.ShowName))
|
|
|
{
|
|
|
message.From.Add(new MailboxAddress(acc.ShowName, acc.MailAccount));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
message.From.Add(MailboxAddress.Parse(acc.MailAccount));
|
|
|
}
|
|
|
|
|
|
//分隔符不统一,统一替换成分号
|
|
|
sendTo = sendTo.Replace(",", ";");
|
|
|
|
|
|
if (sendTo.IndexOf(";") > -1)
|
|
|
{
|
|
|
var arrSendTo = sendTo.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
|
foreach (var s in arrSendTo)
|
|
|
{
|
|
|
var trimStr = s.Trim();
|
|
|
if (!string.IsNullOrEmpty(trimStr))
|
|
|
{
|
|
|
message.To.Add(MailboxAddress.Parse(trimStr));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
message.To.Add(MailboxAddress.Parse(sendTo));
|
|
|
}
|
|
|
|
|
|
message.Subject = subject;
|
|
|
message.Sender = MailboxAddress.Parse(acc.MailAccount);
|
|
|
|
|
|
var html = new TextPart("html")
|
|
|
{
|
|
|
Text = body
|
|
|
};
|
|
|
|
|
|
if (attachList != null && attachList.Length > 0)
|
|
|
{
|
|
|
var mult = new Multipart("mixed") { html };
|
|
|
|
|
|
foreach (var item in attachList)
|
|
|
{
|
|
|
var attPart = new MimePart();
|
|
|
attPart.Content = new MimeContent(new MemoryStream(item.Value));
|
|
|
attPart.ContentDisposition = new ContentDisposition(ContentDisposition.Attachment);
|
|
|
attPart.ContentTransferEncoding = ContentEncoding.Base64;
|
|
|
attPart.FileName = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(item.Key)) + "?=";
|
|
|
mult.Add(attPart);
|
|
|
}
|
|
|
|
|
|
message.Body = mult;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
message.Body = html;
|
|
|
}
|
|
|
|
|
|
using (client = new SmtpClient())
|
|
|
{
|
|
|
_logger.Info($"准备发送邮件{subject} 从【{acc.MailAccount}】到【{sendTo}】,{acc.SmtpServer} {acc.SmtpPort} {acc.SmtpSSL}");
|
|
|
await client.ConnectAsync(acc.SmtpServer, acc.SmtpPort.Value, acc.SmtpSSL.HasValue && acc.SmtpSSL.Value ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None);
|
|
|
await client.AuthenticateAsync(acc.MailAccount, acc.Password);
|
|
|
await client.SendAsync(message);
|
|
|
|
|
|
await client.DisconnectAsync(true);
|
|
|
}
|
|
|
|
|
|
_logger.Info($"mail send success:{subject}");
|
|
|
|
|
|
return DataResult.Successed("发送成功");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.Error($"mail send fail:{subject} 从【{acc.MailAccount}】到【{sendTo}】,{acc.SmtpServer} {acc.SmtpPort} {acc.SmtpSSL}");
|
|
|
_logger.Error(ex.Message);
|
|
|
_logger.Error(ex.StackTrace);
|
|
|
|
|
|
return DataResult.Failed(ex.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|