using MailKit.Net.Smtp;
|
using MimeKit;
|
using MimeKit.Text;
|
using Rhea.Common;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Tiger.Model;
|
|
namespace Tiger.Business
|
{
|
/// <summary>
|
/// 邮件信息
|
/// </summary>
|
public static class MailMessage
|
{
|
/// <summary>
|
/// 组装邮件文本/附件邮件信息
|
/// </summary>
|
/// <param name="mailBodyEntity">邮件消息实体</param>
|
/// <returns></returns>
|
public static MimeMessage AssemblyMailMessage(MailBodyEntity mailBodyEntity)
|
{
|
if (mailBodyEntity == null)
|
{
|
throw new ArgumentNullException(nameof(mailBodyEntity));
|
}
|
|
var message = new MimeMessage();
|
|
//设置邮件基本信息
|
SetMailBaseMessage(message, mailBodyEntity);
|
|
var multipart = new Multipart("mixed");
|
|
//插入文本消息
|
if (string.IsNullOrEmpty(mailBodyEntity.MailTextBody) == false)
|
{
|
var alternative = new MultipartAlternative
|
{
|
AssemblyMailTextMessage(mailBodyEntity.MailTextBody, mailBodyEntity.MailBodyType)
|
};
|
|
multipart.Add(alternative);
|
}
|
|
//插入附件
|
if (mailBodyEntity.MailFilePath != null && File.Exists(mailBodyEntity.MailFilePath) == false)
|
{
|
var mimePart = AssemblyMailAttachmentMessage(mailBodyEntity.MailFileType, mailBodyEntity.MailFileSubType,
|
mailBodyEntity.MailFilePath);
|
|
multipart.Add(mimePart);
|
}
|
|
//组合邮件内容
|
message.Body = multipart;
|
|
return message;
|
}
|
|
/// <summary>
|
/// 设置邮件基础信息
|
/// </summary>
|
/// <param name="minMessag"></param>
|
/// <param name="mailBodyEntity"></param>
|
/// <returns></returns>
|
public static MimeMessage SetMailBaseMessage(MimeMessage minMessag, MailBodyEntity mailBodyEntity)
|
{
|
if (minMessag == null)
|
{
|
throw new ArgumentNullException();
|
}
|
|
if (mailBodyEntity == null)
|
{
|
throw new ArgumentNullException();
|
}
|
|
//插入发件人
|
minMessag.From.Add(new MailboxAddress(mailBodyEntity.Sender, mailBodyEntity.SenderAddress));
|
|
//插入收件人
|
foreach (var recipients in mailBodyEntity.Recipients)
|
{
|
minMessag.To.Add(new MailboxAddress(recipients, recipients));
|
}
|
|
//插入抄送人
|
foreach (var cC in mailBodyEntity.Cc)
|
{
|
minMessag.Cc.Add(new MailboxAddress(cC, cC));
|
}
|
|
//插入主题
|
minMessag.Subject = mailBodyEntity.Subject;
|
|
return minMessag;
|
}
|
|
/// <summary>
|
/// 组装邮件文本信息
|
/// </summary>
|
/// <param name="mailBody">邮件文本内容</param>
|
/// <param name="textPartType">邮件文本类型(plain,html,rtf,xml)</param>
|
/// <returns></returns>
|
public static TextPart AssemblyMailTextMessage(string mailBody, string textPartType)
|
{
|
if (string.IsNullOrEmpty(mailBody))
|
{
|
throw new ArgumentNullException();
|
}
|
|
if (string.IsNullOrEmpty(textPartType))
|
{
|
throw new ArgumentNullException();
|
}
|
|
var textBody = new TextPart(textPartType)
|
{
|
Text = mailBody
|
};
|
|
return textBody;
|
}
|
|
/// <summary>
|
/// 组装邮件附件信息
|
/// </summary>
|
/// <param name="fileAttachmentType">附件类型(image,application)</param>
|
/// <param name="fileAttachmentSubType">附件子类型 </param>
|
/// <param name="fileAttachmentPath">附件路径</param>
|
/// <returns></returns>
|
public static MimePart AssemblyMailAttachmentMessage(string fileAttachmentType, string fileAttachmentSubType, string fileAttachmentPath)
|
{
|
if (string.IsNullOrEmpty(fileAttachmentSubType))
|
{
|
throw new ArgumentNullException();
|
}
|
|
if (string.IsNullOrEmpty(fileAttachmentType))
|
{
|
throw new ArgumentNullException();
|
}
|
|
if (string.IsNullOrEmpty(fileAttachmentPath))
|
{
|
throw new ArgumentNullException();
|
}
|
|
var attachment = new MimePart(fileAttachmentType, fileAttachmentSubType)
|
{
|
Content = new MimeContent(File.OpenRead(fileAttachmentPath)),
|
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
ContentTransferEncoding = ContentEncoding.Base64,
|
FileName = Path.GetFileName(fileAttachmentPath)
|
};
|
|
return attachment;
|
}
|
|
}
|
}
|