邮箱校验功能

master
wanghaomei 1 year ago
parent b8a5e94465
commit d4a9a2ae55

@ -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 });
}
/// <summary>
/// 获取放舱pdf准备作废后期使用打印相关接口
/// </summary>
/// <param name="bookingId"></param>
/// <param name="type">类型1pdf、2xlsx、3docx</param>
/// <returns></returns>
[HttpGet("/BookingOrder/LetterYardPdf")]
public async Task<IActionResult> 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;
}
/// <summary>
/// 获取放舱excel准备作废后期使用打印相关接口
/// </summary>
/// <param name="bookingId"></param>
/// <returns></returns>
[HttpGet("/BookingOrder/LetterYardXlsx")]
public async Task<IActionResult> 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;
}
/// <summary>
/// 生成报表文件(准备作废)
/// </summary>
/// <param name="bookingId"></param>
/// <param name="type">类型1pdf、2xlsx、3docx</param>
/// <param name="typeCode">报表类型代码例如fangcang、samplebill等</param>
/// <returns></returns>
[NonAction]
private async Task<byte[]> 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<PrintTemplateOptions>();
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")}");
}
}
/// <summary>
/// 生成打印报表文件
@ -3483,25 +3354,6 @@ namespace Myshipping.Application
return entity.Id;
}
/// <summary>
/// 获取样单pdf准备作废后期使用打印相关api
/// </summary>
/// <param name="bookingId"></param>
/// <returns></returns>
[HttpGet("/BookingOrder/SampleBillPdf")]
public async Task<IActionResult> 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 小票链接

@ -12311,6 +12311,13 @@
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Myshipping.Core.Service.DjyUserMailAccountService.Check(System.Int64)">
<summary>
校验邮箱
</summary>
<param name="id"></param>
<returns></returns>
</member>
<member name="T:Myshipping.Core.Service.DjyUserMailAccountInput">
<summary>
用户邮箱账号输入参数

@ -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);
}
///// <summary>
///// 获取用户邮箱账号列表
///// </summary>
///// <param name="input"></param>
///// <returns></returns>
//[HttpGet("/DjyUserMailAccount/list")]
//public async Task<dynamic> List([FromQuery] QueryDjyUserMailAccountInput input)
//{
// return await _rep.ToListAsync();
//}
/// <summary>
/// 校验邮箱
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("/DjyUserMailAccount/check")]
public async Task<dynamic> 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 };
}
}
}

Loading…
Cancel
Save