From 55db99f034e59670dacdc909f70e9f2564beba9d Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Wed, 10 May 2023 16:09:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A5=E8=B4=A7=E9=80=9A=E7=9F=A5=E5=8F=91?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BookingOrder/BookingOrderService.cs | 12 ++++----- Myshipping.Core/Helper/MailSendHelper.cs | 26 ++++++++++++++++--- Myshipping.Core/Myshipping.Core.xml | 3 ++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index 62172a9e..d2140832 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -2209,6 +2209,11 @@ namespace Myshipping.Application throw Oops.Bah("放舱信息未找到,请先保存数据"); } + if (string.IsNullOrEmpty(letterYard.AttnMail)) + { + throw Oops.Bah("ATTN MAIL未正确填写"); + } + var mailAcc = await _repUserMail.FirstOrDefaultAsync(x => x.CreatedUserId == user.Id && x.MailAccount == letterYard.AttnMail && x.SmtpPort > 0 && x.SmtpServer != null && x.SmtpServer != ""); if (mailAcc == null) { @@ -2221,11 +2226,6 @@ namespace Myshipping.Application // throw Oops.Bah("未生成链接信息,请重新保存数据"); //} - if (string.IsNullOrEmpty(letterYard.AttnMail)) - { - throw Oops.Bah("ATTN MAIL未正确填写"); - } - #region 保存放舱文件,并挂载到订舱附件 var fileBytes = await GetReportFile(bookingId, "fangcang"); var opt = App.GetOptions(); @@ -2316,7 +2316,7 @@ namespace Myshipping.Application 邮箱:{user.Email}
"; - var sendResult = await MailSendHelper.SendMail(mailAcc, mailSubject, mailContent, letterYard.AttnMail); + var sendResult = await MailSendHelper.SendMail(mailAcc, mailSubject, mailContent, letterYard.AttnMail, new KeyValuePair(fileSaveName, fileBytes)); if (!sendResult.Key) { _logger.LogError($"放舱邮件发送失败:从{mailAcc.MailAccount}到{letterYard.AttnMail},主题 {mailSubject}"); diff --git a/Myshipping.Core/Helper/MailSendHelper.cs b/Myshipping.Core/Helper/MailSendHelper.cs index 39bdf28f..b584dbfa 100644 --- a/Myshipping.Core/Helper/MailSendHelper.cs +++ b/Myshipping.Core/Helper/MailSendHelper.cs @@ -21,7 +21,8 @@ namespace Myshipping.Core.Helper /// /// /// - public static async Task> SendMail(DjyUserMailAccount acc, string subject, string body, string sendTo) + /// 附件列表,key为附件名称,value为文件字节数组 + public static async Task> SendMail(DjyUserMailAccount acc, string subject, string body, string sendTo, params KeyValuePair[] attachList) { SmtpClient client = null; try @@ -58,16 +59,33 @@ namespace Myshipping.Core.Helper } message.Subject = subject; + message.Sender = MailboxAddress.Parse(acc.MailAccount); + var html = new TextPart("html") { Text = body }; - MimeEntity entity = html; + if (attachList != null && attachList.Length > 0) + { + var mult = new Multipart("mixed") { html }; - message.Sender = MailboxAddress.Parse(acc.MailAccount); - message.Body = entity; + 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()) { diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index f9b4de62..49a38200 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -5557,7 +5557,7 @@ 标题 内容 - + 使用用户邮箱配置发送邮件 @@ -5565,6 +5565,7 @@ + 附件列表,key为附件名称,value为文件字节数组