|
|
|
|
using Furion.Logging;
|
|
|
|
|
using MailKit.Net.Smtp;
|
|
|
|
|
using MailKit.Security;
|
|
|
|
|
using MimeKit;
|
|
|
|
|
using Myshipping.Core.Entity;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Myshipping.Core.Helper
|
|
|
|
|
{
|
|
|
|
|
public static class MailSendHelper
|
|
|
|
|
{
|
|
|
|
|
/// <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<KeyValuePair<bool, string>> SendMail(DjyUserMailAccount 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())
|
|
|
|
|
{
|
|
|
|
|
Log.Information($"准备发送邮件{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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Information($"mail send success:{subject}");
|
|
|
|
|
|
|
|
|
|
return new KeyValuePair<bool, string>(true, "发送成功");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Error($"mail send fail:{subject} 从【{acc.MailAccount}】到【{sendTo}】,{acc.SmtpServer} {acc.SmtpPort} {acc.SmtpSSL}");
|
|
|
|
|
Log.Error(ex.Message);
|
|
|
|
|
Log.Error(ex.StackTrace);
|
|
|
|
|
|
|
|
|
|
return new KeyValuePair<bool, string>(false, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|