using MimeKit;
using MimeKit.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonTool.MailKit
{
///
/// 邮件信息
///
public static class MailMessage
{
///
/// 组装邮件文本/附件邮件信息
///
/// 邮件消息实体
///
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.Body))
{
var alternative = new MultipartAlternative
{
AssemblyMailTextMessage(mailBodyEntity.Body, mailBodyEntity.MailBodyType)
};
multipart.Add(alternative);
}
//插入附件
foreach (var mailFile in mailBodyEntity.MailFiles)
{
if (mailFile.MailFilePath != null && File.Exists(mailFile.MailFilePath))
{
var mimePart = AssemblyMailAttachmentMessage(mailFile.MailFileType, mailFile.MailFileSubType,
mailFile.MailFilePath);
multipart.Add(mimePart);
}
}
//组合邮件内容
message.Body = multipart;
return message;
}
///
/// 设置邮件基础信息
///
///
///
///
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));
//插入收件人
if (mailBodyEntity.Recipients.Any())
{
foreach (var recipients in mailBodyEntity.Recipients)
{
minMessag.To.Add(new MailboxAddress(recipients));
}
}
//插入抄送人
if (mailBodyEntity.Cc != null && mailBodyEntity.Cc.Any())
{
foreach (var cC in mailBodyEntity.Cc)
{
minMessag.Cc.Add(new MailboxAddress(cC));
}
}
//插入密送人
if (mailBodyEntity.Bcc != null && mailBodyEntity.Bcc.Any())
{
foreach (var bcc in mailBodyEntity.Bcc)
{
minMessag.Bcc.Add(new MailboxAddress(bcc));
}
}
//插入主题
minMessag.Subject = mailBodyEntity.Subject;
return minMessag;
}
///
/// 组装邮件文本信息
///
/// 邮件内容
/// 邮件类型(plain,html,rtf,xml)
///
public static TextPart AssemblyMailTextMessage(string mailBody, TextFormat textPartType)
{
if (string.IsNullOrEmpty(mailBody))
{
throw new ArgumentNullException();
}
//var textBody = new TextPart(textPartType)
//{
// Text = mailBody,
//};
//处理查看源文件有乱码问题
var textBody = new TextPart(textPartType);
textBody.SetText(Encoding.Default, mailBody);
return textBody;
}
///
/// 组装邮件附件信息
///
/// 附件类型(image,application)
/// 附件子类型
/// 附件路径
///
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 fileName = Path.GetFileName(fileAttachmentPath);
var attachment = new MimePart(fileAttachmentType, fileAttachmentSubType)
{
Content = new MimeContent(File.OpenRead(fileAttachmentPath)),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
//FileName = fileName,
};
//qq邮箱附件文件名中文乱码问题
//var charset = "GB18030";
attachment.ContentType.Parameters.Add(Encoding.Default, "name", fileName);
attachment.ContentDisposition.Parameters.Add(Encoding.Default, "filename", fileName);
//处理文件名过长
foreach (var param in attachment.ContentDisposition.Parameters)
param.EncodingMethod = ParameterEncodingMethod.Rfc2047;
foreach (var param in attachment.ContentType.Parameters)
param.EncodingMethod = ParameterEncodingMethod.Rfc2047;
return attachment;
}
///
/// 创建邮件日志文件
///
///
public static string CreateMailLog()
{
var logPath = AppDomain.CurrentDomain.BaseDirectory + "/DocumentLog/" +
Guid.NewGuid() + ".txt";
if (File.Exists(logPath)) return logPath;
var fs = File.Create(logPath);
fs.Close();
return logPath;
}
}
}