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 { /// /// 邮件信息 /// 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.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; } /// /// 设置邮件基础信息 /// /// /// /// 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; } /// /// 组装邮件文本信息 /// /// 邮件文本内容 /// 邮件文本类型(plain,html,rtf,xml) /// 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; } /// /// 组装邮件附件信息 /// /// 附件类型(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 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; } } }