去除多余的逻辑
parent
e0e6e60ae0
commit
5092f5204f
|
|
@ -687,255 +687,6 @@ public class EmailLogService(IRepository<EmailLog> _emailLogRepository,
|
|||
}
|
||||
|
||||
|
||||
//private string AcquireOAuth2TokenByPassword()
|
||||
//{
|
||||
// if (_systemEmailConfig.OAuthTenantId.IsNullOrEmpty() || _systemEmailConfig.OAuthClientId.IsNullOrEmpty())
|
||||
// return string.Empty;
|
||||
|
||||
// using var http = new HttpClient();
|
||||
// var req = new PasswordTokenRequest
|
||||
// {
|
||||
// Address = $"https://login.microsoftonline.com/{_systemEmailConfig.OAuthTenantId}/oauth2/v2.0/token",
|
||||
// ClientId = _systemEmailConfig.OAuthClientId,
|
||||
// ClientSecret = _systemEmailConfig.OAuthClientSecret,
|
||||
// Scope = "offline_access https://outlook.office365.com/IMAP.AccessAsUser.All",
|
||||
// UserName = _systemEmailConfig.FromEmail,
|
||||
// Password = _systemEmailConfig.AuthorizationCode
|
||||
// };
|
||||
// var resp = http.RequestPasswordTokenAsync(req).GetAwaiter().GetResult();
|
||||
// if (resp.IsError || resp.AccessToken.IsNullOrEmpty())
|
||||
// return string.Empty;
|
||||
// return resp.AccessToken;
|
||||
//}
|
||||
|
||||
// 取skip的值
|
||||
public int CalcReverseSkip(int pageIndex, int pageSize, int totalCount)
|
||||
{
|
||||
|
||||
int lastPageFullSkip = totalCount / pageSize * pageSize; // 最后一页“整页”起点
|
||||
int skip = lastPageFullSkip - pageIndex * pageSize; // 倒着减
|
||||
return Math.Max(skip, 0); // 防止负值
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 邮件筛选条件构建
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
private SearchQuery BuildSearchQuery(EmailLogQuery query)
|
||||
{
|
||||
var searchQuery = SearchQuery.All;
|
||||
|
||||
// 日期范围筛选
|
||||
if (query.EmailStartDate.HasValue || query.EmailEndDate.HasValue)
|
||||
{
|
||||
var startDate = query.EmailStartDate ?? DateTime.MinValue;
|
||||
var endDate = query.EmailEndDate ?? DateTime.MaxValue;
|
||||
|
||||
searchQuery = searchQuery.And(SearchQuery.DeliveredAfter(startDate)
|
||||
.And(SearchQuery.DeliveredBefore(endDate.AddDays(1)))); // 包含结束日期当天
|
||||
}
|
||||
|
||||
// 主题关键词筛选(如果IMAP服务器支持)
|
||||
if (!string.IsNullOrEmpty(query.ToRecipientName))
|
||||
{
|
||||
searchQuery = searchQuery.And(SearchQuery.HeaderContains("To", query.ToRecipientName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(query.CcRecipientName))
|
||||
{
|
||||
searchQuery = searchQuery.And(SearchQuery.HeaderContains("Cc", query.CcRecipientName));
|
||||
}
|
||||
|
||||
return searchQuery;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 邮件排序处理
|
||||
/// </summary>
|
||||
/// <param name="sentFolder"></param>
|
||||
/// <param name="uids"></param>
|
||||
/// <returns></returns>
|
||||
private List<EmailLogView> GetEmailsWithSorting(IMailFolder sentFolder, IList<UniqueId> uids)
|
||||
{
|
||||
var emailList = new List<EmailLogView>();
|
||||
|
||||
|
||||
|
||||
// 获取完整的邮件信息
|
||||
foreach (var uid in uids)
|
||||
{
|
||||
try
|
||||
{
|
||||
var message = sentFolder.GetMessage(uid);
|
||||
var emailView = ConvertToEmailLogView(uid, message);
|
||||
emailList.Add(emailView);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"处理邮件 {uid} 失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return emailList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取完整的邮件信息
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
private EmailLogView ConvertToEmailLogView(UniqueId uid, MimeMessage message)
|
||||
{
|
||||
string saveFolder = @"D:\MailAttachments";
|
||||
Directory.CreateDirectory(saveFolder);
|
||||
var emailView = new EmailLogView
|
||||
{
|
||||
UniqueId = uid.ToString(),
|
||||
MessageId = message.MessageId ?? string.Empty,
|
||||
EmailSubject = message.Subject ?? string.Empty,
|
||||
EmailDate = message.Date.UtcDateTime,
|
||||
Content = message.HtmlBody ?? string.Empty,
|
||||
};
|
||||
|
||||
var toHeader = message.Headers[HeaderId.To]; // 原始头,未解码
|
||||
Console.WriteLine($"原始To头 = |{toHeader}|");
|
||||
|
||||
foreach (var att in message.Attachments)
|
||||
{
|
||||
// att 可能是 MimePart 或 MessagePart
|
||||
if (att is MimePart part && !string.IsNullOrEmpty(part.FileName))
|
||||
{
|
||||
var file = Path.Combine(saveFolder, part.FileName);
|
||||
using (var stream = File.Create(file))
|
||||
part.Content.DecodeTo(stream);
|
||||
Console.WriteLine($" 附件已保存:{file}");
|
||||
}
|
||||
}
|
||||
|
||||
// 处理发件人信息
|
||||
var fromMailbox = message.From.Mailboxes.FirstOrDefault();
|
||||
if (fromMailbox != null)
|
||||
{
|
||||
emailView.SenderAddress = fromMailbox.Address;
|
||||
emailView.SenderName = fromMailbox.Name ?? string.Empty;
|
||||
}
|
||||
|
||||
//// 处理收件人
|
||||
//emailView.ToRecipients = message.To.Mailboxes.Select(address => new EmaliSendInfo
|
||||
//{
|
||||
// Name = address.Name ?? string.Empty,
|
||||
// Address = address.Address
|
||||
//}).ToList();
|
||||
|
||||
//// 处理抄送人
|
||||
//emailView.CcRecipients = message.Cc.Mailboxes.Select(address => new EmaliSendInfo
|
||||
//{
|
||||
// Name = address.Name ?? string.Empty,
|
||||
// Address = address.Address
|
||||
//}).ToList();
|
||||
|
||||
//// 处理密送人
|
||||
//emailView.BccRecipients = message.Bcc.Mailboxes.Select(address => new EmaliSendInfo
|
||||
//{
|
||||
// Name = address.Name ?? string.Empty,
|
||||
// Address = address.Address
|
||||
//}).ToList();
|
||||
|
||||
// 处理附件
|
||||
//emailView.Attachments = ProcessAttachments(message);
|
||||
//emailView.HasAttachments = emailView.Attachments.Any();
|
||||
//emailView.AttachmentCount = emailView.Attachments.Count;
|
||||
|
||||
return emailView;
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 处理附件信息
|
||||
///// </summary>
|
||||
///// <param name="message"></param>
|
||||
///// <returns></returns>
|
||||
//private List<EmailAttachmentInfo> ProcessAttachments(MimeMessage message)
|
||||
//{
|
||||
// var attachments = new List<EmailAttachmentInfo>();
|
||||
|
||||
// foreach (var attachment in message.Attachments)
|
||||
// {
|
||||
// var attachmentInfo = new EmailAttachmentInfo();
|
||||
|
||||
// if (attachment is MimePart mimePart)
|
||||
// {
|
||||
// attachmentInfo.FileName = mimePart.FileName ??
|
||||
// mimePart.ContentType?.Name ??
|
||||
// "未知文件";
|
||||
// attachmentInfo.ContentType = mimePart.ContentType.MimeType;
|
||||
// attachmentInfo.ContentId = mimePart.ContentId ?? string.Empty;
|
||||
// attachmentInfo.IsInline = mimePart.ContentDisposition != null &&
|
||||
// mimePart.ContentDisposition.Disposition.Equals("inline", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
// // 获取附件大小
|
||||
// try
|
||||
// {
|
||||
// if (mimePart.ContentObject.Stream != null)
|
||||
// {
|
||||
// attachmentInfo.Size = mimePart.ContentObject.Stream.Length;
|
||||
// }
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// attachmentInfo.Size = 0;
|
||||
// }
|
||||
// }
|
||||
// else if (attachment is MessagePart messagePart)
|
||||
// {
|
||||
// // 处理内嵌消息作为附件的情况
|
||||
// attachmentInfo.FileName = "embedded-message.eml";
|
||||
// attachmentInfo.ContentType = "message/rfc822";
|
||||
// attachmentInfo.IsInline = false;
|
||||
// }
|
||||
|
||||
// attachments.Add(attachmentInfo);
|
||||
// }
|
||||
|
||||
// return attachments;
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 应用内存中的过滤条件(对于无法通过IMAP搜索的条件)
|
||||
/// </summary>
|
||||
/// <param name="emails"></param>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
private List<EmailLogView> ApplyMemoryFilters(List<EmailLogView> emails, EmailLogQuery query)
|
||||
{
|
||||
var filtered = emails.AsEnumerable();
|
||||
|
||||
// 收件人姓名筛选
|
||||
//if (!string.IsNullOrEmpty(query.Keyword))
|
||||
//{
|
||||
// filtered = filtered.Where(e =>
|
||||
// e.ToRecipients.Any(r =>
|
||||
// r.Name.Contains(query.Keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
// e.CcRecipients.Any(r =>
|
||||
// r.Name.Contains(query.Keyword, StringComparison.OrdinalIgnoreCase))
|
||||
|
||||
// );
|
||||
//}
|
||||
|
||||
// 发件人姓名筛选
|
||||
//if (!string.IsNullOrEmpty(query.SenderName))
|
||||
//{
|
||||
// filtered = filtered.Where(e =>
|
||||
// e.SenderName.Contains(query.SenderName, StringComparison.OrdinalIgnoreCase));
|
||||
//}
|
||||
|
||||
return filtered.ToList();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IResponseOutput> AddOrUpdateEmailLog(EmailLogAddOrEdit addOrEditEmailLog)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue