From d4a9a2ae553b333b58facc5cd1701a636ed185e7 Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Thu, 12 Oct 2023 14:44:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E7=AE=B1=E6=A0=A1=E9=AA=8C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BookingOrder/BookingOrderService.cs | 150 +----------------- Myshipping.Core/Myshipping.Core.xml | 7 + .../DjyUserMailAccountService.cs | 128 +++++++++++++-- 3 files changed, 126 insertions(+), 159 deletions(-) diff --git a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs index c8bf59b..e46fa5d 100644 --- a/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs +++ b/Myshipping.Application/Service/BookingOrder/BookingOrderService.cs @@ -2359,7 +2359,7 @@ namespace Myshipping.Application catch { } } - await SendLetterYard(input.BookingId); + //await SendLetterYard(input.BookingId); return rtnId; } @@ -2624,135 +2624,6 @@ namespace Myshipping.Application await SendBookingOrder(new long[] { bookingId }); } - /// - /// 获取放舱pdf(准备作废,后期使用打印相关接口) - /// - /// - /// 类型,1:pdf、2:xlsx、3:docx - /// - [HttpGet("/BookingOrder/LetterYardPdf")] - public async Task LetterYardPdf(long bookingId, int type = 1) - { - var letterYard = await _repLetterYard.FirstOrDefaultAsync(x => x.BookingId == bookingId); - if (letterYard == null) - { - throw Oops.Bah("放舱信息未找到,请先保存数据"); - } - - var fileType = ""; - if (type == 1) - { - fileType = ".pdf"; - } - else if (type == 2) - { - fileType = ".xlsx"; - } - else if (type == 3) - { - fileType = ".docx"; - } - else - { - throw Oops.Bah("类型参数不正确"); - } - - var bs = await GetReportFile(bookingId, "fangcang", type); - var fileName = HttpUtility.UrlEncode($"{bookingId}_{DateTime.Now.Ticks}{fileType}", Encoding.GetEncoding("UTF-8")); - var result = new FileContentResult(bs, "application/octet-stream") { FileDownloadName = fileName }; - return result; - - } - - /// - /// 获取放舱excel(准备作废,后期使用打印相关接口) - /// - /// - /// - [HttpGet("/BookingOrder/LetterYardXlsx")] - public async Task LetterYardXlsx(long bookingId) - { - var letterYard = await _repLetterYard.FirstOrDefaultAsync(x => x.BookingId == bookingId); - if (letterYard == null) - { - throw Oops.Bah("放舱信息未找到,请先保存数据"); - } - var bs = await GetReportFile(bookingId, "fangcang", 2); - var fileName = HttpUtility.UrlEncode($"{bookingId}_{DateTime.Now.Ticks}.xlsx", Encoding.GetEncoding("UTF-8")); - var result = new FileContentResult(bs, "application/octet-stream") { FileDownloadName = fileName }; - return result; - } - - /// - /// 生成报表文件(准备作废) - /// - /// - /// 类型,1:pdf、2:xlsx、3:docx - /// 报表类型代码,例如fangcang、samplebill等 - /// - [NonAction] - private async Task GetReportFile(long bookingId, string typeCode, int type = 1) - { - //打印报表服务地址 - var reportUrl = _cache.GetAllDictData().Result.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "url_report_generate").Value; - if (!reportUrl.EndsWith("/")) - { - reportUrl += "/"; - } - - //订舱数据 - var order = _rep.FirstOrDefault(x => x.Id == bookingId); - if (order == null) - { - throw Oops.Bah(BookingErrorCode.BOOK001); - } - - _logger.LogInformation($"查找模板:bookingId-{bookingId} TenantId-{order.TenantId}"); - var printTemplate = await _repPrintTemplate.AsQueryable().Filter(null, true).FirstAsync(x => x.TenantId == order.TenantId && x.TypeCode == typeCode); - if (printTemplate == null) - { - throw Oops.Bah(BookingErrorCode.BOOK115); - } - - var opt = App.GetOptions(); - var dirAbs = opt.basePath; - if (string.IsNullOrEmpty(dirAbs)) - { - dirAbs = App.WebHostEnvironment.WebRootPath; - } - - var fileAbsPath = Path.Combine(dirAbs, printTemplate.FilePath); - _logger.LogInformation($"查找模板文件:{fileAbsPath}"); - if (!File.Exists(fileAbsPath)) - { - throw Oops.Bah(BookingErrorCode.BOOK115); - } - - _logger.LogInformation($"准备调用报表生成:id:{bookingId},文件:{printTemplate.FileName}"); - var genUrl = $"{reportUrl}Report/BookingReport?bookingId={bookingId}&type={type}"; - var rtn = await genUrl - .SetContentType("multipart/form-data") - .SetBodyBytes(("file", File.ReadAllBytes(fileAbsPath), HttpUtility.UrlEncode(printTemplate.FileName, Encoding.GetEncoding("UTF-8")))) - .PostAsStringAsync(); - var jobjRtn = JObject.Parse(rtn); - _logger.LogInformation($"调用报表生成返回:{rtn}"); - if (jobjRtn.GetBooleanValue("Success")) - { - //调用读取文件 - var fn = jobjRtn.GetStringValue("Data"); - _logger.LogInformation($"准备调用读取报表文件:id:{bookingId},文件名:{fn}"); - var readFileUrl = $"{reportUrl}Report/GetFile?fileName={fn}"; - var bs = await readFileUrl.GetAsByteArrayAsync(); - - _logger.LogInformation($"调用读取报表文件返回:{bs.Length}"); - - return bs; - } - else - { - throw Oops.Bah($"生成报表文件失败:{jobjRtn.GetStringValue("Message")}"); - } - } /// /// 生成打印报表文件 @@ -3483,25 +3354,6 @@ namespace Myshipping.Application return entity.Id; } - /// - /// 获取样单pdf(准备作废,后期使用打印相关api) - /// - /// - /// - [HttpGet("/BookingOrder/SampleBillPdf")] - public async Task SampleBillPdf(long bookingId) - { - var samp = await _repSampleBill.FirstOrDefaultAsync(x => x.BookingId == bookingId); - if (samp == null) - { - throw Oops.Bah("请先保存提单确认数据后再继续"); - } - - var bs = await GetReportFile(bookingId, "tidanqueren"); - var fileName = HttpUtility.UrlEncode($"{bookingId}_{DateTime.Now.Ticks}.xlsx", Encoding.GetEncoding("UTF-8")); - var result = new FileContentResult(bs, "application/octet-stream") { FileDownloadName = fileName }; - return result; - } #endregion #region 小票链接 diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index 92447f0..ad37dd5 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -12311,6 +12311,13 @@ + + + 校验邮箱 + + + + 用户邮箱账号输入参数 diff --git a/Myshipping.Core/Service/DjyUserMailAccount/DjyUserMailAccountService.cs b/Myshipping.Core/Service/DjyUserMailAccount/DjyUserMailAccountService.cs index 394f297..243ed60 100644 --- a/Myshipping.Core/Service/DjyUserMailAccount/DjyUserMailAccountService.cs +++ b/Myshipping.Core/Service/DjyUserMailAccount/DjyUserMailAccountService.cs @@ -9,6 +9,13 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Furion.FriendlyException; using Myshipping.Core.Entity; +using MailKit.Net.Smtp; +using MailKit.Security; +using MimeKit; +using System; +using MailKit.Net.Pop3; +using MailKit.Net.Imap; +using MailKit; namespace Myshipping.Core.Service { @@ -101,15 +108,116 @@ namespace Myshipping.Core.Service return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id); } - ///// - ///// 获取用户邮箱账号列表 - ///// - ///// - ///// - //[HttpGet("/DjyUserMailAccount/list")] - //public async Task List([FromQuery] QueryDjyUserMailAccountInput input) - //{ - // return await _rep.ToListAsync(); - //} + /// + /// 校验邮箱 + /// + /// + /// + [HttpGet("/DjyUserMailAccount/check")] + public async Task Check(long id) + { + var ma = await _rep.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id); + if (ma == null) + { + throw Oops.Oh("邮箱账号未找到"); + } + + if (string.IsNullOrEmpty(ma.MailAccount) + || string.IsNullOrEmpty(ma.Password) + || string.IsNullOrEmpty(ma.ReceiveServer) + || string.IsNullOrEmpty(ma.SmtpServer) + || !ma.ReceivePort.HasValue + || !ma.SmtpPort.HasValue) + { + throw Oops.Oh("请完整填写邮箱的账号、密码、收发件服务器及端口等信息"); + } + + var recOK = false; + var sendOK = false; + + //接收 + if (ma.UseImap == true) + { + try + { + using (var imapClient = new ImapClient()) + { + imapClient.ServerCertificateValidationCallback = (s, c, h, e) => true; + _logger.LogInformation($"正在验证IMAP收件服务器:{ma.ReceiveServer}:{ma.ReceivePort},SSL:{ma.ReceiveSSL}"); + imapClient.Connect(ma.ReceiveServer, ma.ReceivePort.Value, ma.ReceiveSSL == true ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None); + _logger.LogInformation($"正在登录邮件服务器,账号:{ma.MailAccount}"); + imapClient.Authenticate(ma.MailAccount, ma.Password); + + var inbox = imapClient.Inbox; + inbox.Open(FolderAccess.ReadOnly); + _logger.LogInformation($"获取IMAP Folder成功,账号:{ma.MailAccount}"); + inbox.Close(); + + imapClient.Disconnect(true); + recOK = true; + } + } + catch (Exception ex) + { + _logger.LogInformation($"验证 {ma.MailAccount} IMAP收件服务器失败:{ex.Message}"); + } + } + else + { + try + { + using (var popClient = new Pop3Client()) + { + popClient.ServerCertificateValidationCallback = (s, c, h, e) => true; + _logger.LogInformation($"正在验证POP3收件服务器:{ma.ReceiveServer}:{ma.ReceivePort},SSL:{ma.ReceiveSSL}"); + popClient.Connect(ma.ReceiveServer, ma.ReceivePort.Value, ma.ReceiveSSL == true ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None); + _logger.LogInformation($"正在登录邮件服务器,账号:{ma.MailAccount}"); + popClient.Authenticate(ma.MailAccount, ma.Password); + + var uids = popClient.GetMessageUids(); + _logger.LogInformation($"获取POP3 UID列表成功,账号:{ma.MailAccount}"); + + popClient.Disconnect(true); + recOK = true; + } + } + catch (Exception ex) + { + _logger.LogInformation($"验证 {ma.MailAccount} POP3收件服务器失败:{ex.Message}"); + } + } + + //发送 + try + { + using (var client = new SmtpClient()) + { + _logger.LogInformation($"准备校验发送邮件{ma.MailAccount} {ma.SmtpServer} {ma.SmtpPort} {ma.SmtpSSL}"); + await client.ConnectAsync(ma.SmtpServer, ma.SmtpPort.Value, ma.SmtpSSL.HasValue && ma.SmtpSSL.Value ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None); + await client.AuthenticateAsync(ma.MailAccount, ma.Password); + + //var message = new MimeMessage(); + //message.From.Add(MailboxAddress.Parse(ma.MailAccount)); + //message.To.Add(MailboxAddress.Parse(ma.MailAccount)); + //message.Subject = "大简云邮件发送校验"; + //message.Sender = MailboxAddress.Parse(ma.MailAccount); + //var html = new TextPart("html") + //{ + // Text = "大简云邮件发送校验" + //}; + //message.MessageId = Guid.NewGuid().ToString(); + //await client.SendAsync(message); + await client.DisconnectAsync(true); + + sendOK = true; + } + } + catch (Exception ex) + { + _logger.LogInformation($"验证 {ma.MailAccount} POP3收件服务器失败:{ex.Message}"); + } + + return new { recOK, sendOK }; + } } }