From 1427940224212c2a557de18bf9b1136cec8ba9b0 Mon Sep 17 00:00:00 2001 From: he <109787524@qq.com> Date: Mon, 8 Dec 2025 13:36:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=97=A5=E5=BF=97=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Common/DTO/EmailLogViewModel.cs | 15 ++++++ .../Service/Common/EmailLogService.cs | 49 ++++++++++++------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/IRaCIS.Core.Application/Service/Common/DTO/EmailLogViewModel.cs b/IRaCIS.Core.Application/Service/Common/DTO/EmailLogViewModel.cs index 4ee085d5b..247908408 100644 --- a/IRaCIS.Core.Application/Service/Common/DTO/EmailLogViewModel.cs +++ b/IRaCIS.Core.Application/Service/Common/DTO/EmailLogViewModel.cs @@ -130,12 +130,27 @@ public class GetEmailInfoOutDto:EmailLogView public class GetEmailInfoInDto { public Guid Id { get; set; } + + public Guid? TrialId { get; set; } } +public class SynchronizationEmailInDto +{ + public Guid? TrialId { get; set; } +} public class ResendEmailInDto { public Guid Id { get; set; } + + public Guid? TrialId { get; set; } +} + +public class EmailAuthorization +{ + public string FromEmail { get; set; } = string.Empty; + + public string AuthorizationCode { get; set; } = string.Empty; } public class EmailLogQuery:PageInput diff --git a/IRaCIS.Core.Application/Service/Common/EmailLogService.cs b/IRaCIS.Core.Application/Service/Common/EmailLogService.cs index 423e5dbf5..15eebf6b1 100644 --- a/IRaCIS.Core.Application/Service/Common/EmailLogService.cs +++ b/IRaCIS.Core.Application/Service/Common/EmailLogService.cs @@ -23,6 +23,7 @@ using System.IO; using System.Linq; using System.Net.Mail; using System.Threading.Tasks; +using Panda.DynamicWebApi.Attributes; namespace IRaCIS.Core.Application.Service; /// @@ -54,14 +55,14 @@ public class EmailLogService(IRepository _emailLogRepository, [HttpPost] public async Task> GetEmailLogList(EmailLogQuery inDto) { - var emailFromName = await _trialRepository.Where(x=>x.Id==inDto.TrialId).Select(x=>x.EmailFromName).FirstOrDefaultAsync(); + var emailFromEmail = await _trialRepository.Where(x=>x.Id==inDto.TrialId).Select(x=>x.EmailFromEmail).FirstOrDefaultAsync(); - if (emailFromName.IsNullOrEmpty()) + if (emailFromEmail.IsNullOrEmpty()) { - emailFromName = _systemEmailConfig.FromName; + emailFromEmail = _systemEmailConfig.FromEmail; } var emailLogQueryable = _emailLogRepository - .Where(x=>x.SenderName== emailFromName) + .Where(x=>x.SenderAddress== emailFromEmail) .WhereIf(inDto.EmailStartDate.HasValue, x => x.EmailDate >= inDto.EmailStartDate.Value) .WhereIf(inDto.EmailEndDate.HasValue, x => x.EmailDate <= inDto.EmailEndDate.Value) .WhereIf(inDto.EmailStateEnum.HasValue, x => x.EmailStateEnum == inDto.EmailStateEnum.Value) @@ -119,8 +120,7 @@ public class EmailLogService(IRepository _emailLogRepository, { try { - client.Connect(_systemEmailConfig.Imap, _systemEmailConfig.ImapPort, SecureSocketOptions.SslOnConnect); - AuthenticateImap(client); + await AuthenticateImap(client,inDto.TrialId); var sentFolder = OpenSentFolder(client); var uid = new UniqueId(uint.Parse(emailInfo.UniqueId)); var message = sentFolder.GetMessage(uid); @@ -205,8 +205,7 @@ public class EmailLogService(IRepository _emailLogRepository, { try { - client.Connect(_systemEmailConfig.Imap, _systemEmailConfig.ImapPort, SecureSocketOptions.SslOnConnect); - AuthenticateImap(client); + await AuthenticateImap(client,inDto.TrialId); var sentFolder = OpenSentFolder(client); var uid = new UniqueId(uint.Parse(emailInfo.UniqueId)); var message = sentFolder.GetMessage(uid); @@ -267,7 +266,7 @@ public class EmailLogService(IRepository _emailLogRepository, /// /// [HttpPost] - public async Task SynchronizationEmail() + public async Task SynchronizationEmail(SynchronizationEmailInDto inDto) { var maxTime = await _emailLogRepository.MaxAsync(t => t.EmailDate); var startDate = maxTime ?? DateTime.MinValue; @@ -279,8 +278,8 @@ public class EmailLogService(IRepository _emailLogRepository, { try { - client.Connect(_systemEmailConfig.Imap, _systemEmailConfig.ImapPort, SecureSocketOptions.SslOnConnect); - AuthenticateImap(client); + + await AuthenticateImap(client, inDto.TrialId); var sentFolder = OpenSentFolder(client); var searchQuery = SearchQuery.All.And(SearchQuery.DeliveredAfter(startDate)); @@ -368,7 +367,7 @@ public class EmailLogService(IRepository _emailLogRepository, // 第二步:处理收件箱中的邮件发送失败通知 try { - await ProcessInboxFailureNotifications(startDate); + await ProcessInboxFailureNotifications(startDate,inDto.TrialId); } catch (Exception ex) { @@ -490,11 +489,27 @@ public class EmailLogService(IRepository _emailLogRepository, return folder; } - private void AuthenticateImap(ImapClient client) + private async Task AuthenticateImap(ImapClient client, Guid? trialId) { try { - client.Authenticate(_systemEmailConfig.FromEmail, _systemEmailConfig.AuthorizationCode); + EmailAuthorization authorization = new EmailAuthorization() + { + FromEmail = _systemEmailConfig.FromEmail, + AuthorizationCode = _systemEmailConfig.AuthorizationCode, + }; + + if (trialId != null) + { + authorization = await _trialRepository.Where(x => x.Id == trialId.Value).Select(x => new EmailAuthorization() + { + AuthorizationCode = x.EmailAuthorizationCode, + FromEmail = x.EmailFromEmail + }).FirstNotNullAsync(); + } + + client.Connect(_systemEmailConfig.Imap, _systemEmailConfig.ImapPort, SecureSocketOptions.SslOnConnect); + client.Authenticate(authorization.FromEmail, authorization.AuthorizationCode); } catch (AuthenticationException) { @@ -523,14 +538,14 @@ public class EmailLogService(IRepository _emailLogRepository, /// 处理收件箱中的邮件发送失败通知 /// /// - public async Task ProcessInboxFailureNotifications(DateTime startTime) + public async Task ProcessInboxFailureNotifications(DateTime startTime, Guid? trialId) { using (var client = new ImapClient()) { try { - client.Connect(_systemEmailConfig.Imap, _systemEmailConfig.ImapPort, SecureSocketOptions.SslOnConnect); - AuthenticateImap(client); + + await AuthenticateImap(client, trialId); var inboxFolder = OpenInboxFolder(client); // 搜索可能包含失败通知的邮件