Compare commits
No commits in common. "2e46791466437157aef9842a13c673ea52a201d7" and "1de687cbf3a76671a5181976b358011866fd6c75" have entirely different histories.
2e46791466
...
1de687cbf3
|
|
@ -10,7 +10,6 @@ using IRaCIS.Core.Application.ViewModel;
|
||||||
using IRaCIS.Core.Domain.Models;
|
using IRaCIS.Core.Domain.Models;
|
||||||
using IRaCIS.Core.Infra.EFCore;
|
using IRaCIS.Core.Infra.EFCore;
|
||||||
using IRaCIS.Core.Infrastructure.Extention;
|
using IRaCIS.Core.Infrastructure.Extention;
|
||||||
using IdentityModel.Client;
|
|
||||||
using MailKit;
|
using MailKit;
|
||||||
using MailKit.Net.Imap;
|
using MailKit.Net.Imap;
|
||||||
using MailKit.Search;
|
using MailKit.Search;
|
||||||
|
|
@ -96,11 +95,12 @@ public class EmailLogService(IRepository<EmailLog> _emailLogRepository,
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
||||||
AuthenticateImap(client);
|
client.Authenticate(_systemEmailConfig.FromEmail, _systemEmailConfig.AuthorizationCode);
|
||||||
var sentFolder = OpenSentFolder(client);
|
var sentFolder = client.GetFolder("已发送");
|
||||||
|
sentFolder.Open(FolderAccess.ReadOnly);
|
||||||
var uid = new UniqueId(uint.Parse(emailInfo.UniqueId));
|
var uid = new UniqueId(uint.Parse(emailInfo.UniqueId));
|
||||||
var message = sentFolder.GetMessage(uid);
|
var message = sentFolder.GetMessage(uid);
|
||||||
emailInfo.Content = message.HtmlBody ?? message.TextBody ?? string.Empty;
|
emailInfo.Content = message.HtmlBody ?? string.Empty;
|
||||||
|
|
||||||
|
|
||||||
if (emailInfo.AttachmentList.Count == 0)
|
if (emailInfo.AttachmentList.Count == 0)
|
||||||
|
|
@ -182,11 +182,12 @@ public class EmailLogService(IRepository<EmailLog> _emailLogRepository,
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
||||||
AuthenticateImap(client);
|
client.Authenticate(_systemEmailConfig.FromEmail, _systemEmailConfig.AuthorizationCode);
|
||||||
var sentFolder = OpenSentFolder(client);
|
var sentFolder = client.GetFolder("已发送");
|
||||||
|
sentFolder.Open(FolderAccess.ReadOnly);
|
||||||
var uid = new UniqueId(uint.Parse(emailInfo.UniqueId));
|
var uid = new UniqueId(uint.Parse(emailInfo.UniqueId));
|
||||||
var message = sentFolder.GetMessage(uid);
|
var message = sentFolder.GetMessage(uid);
|
||||||
emailInfo.Content = message.HtmlBody ?? message.TextBody ?? string.Empty;
|
emailInfo.Content = message.HtmlBody ?? string.Empty;
|
||||||
sentFolder.Close();
|
sentFolder.Close();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -245,8 +246,9 @@ public class EmailLogService(IRepository<EmailLog> _emailLogRepository,
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
client.Connect(_systemEmailConfig.Imap, 993, SecureSocketOptions.SslOnConnect);
|
||||||
AuthenticateImap(client);
|
client.Authenticate(_systemEmailConfig.FromEmail, _systemEmailConfig.AuthorizationCode);
|
||||||
var sentFolder = OpenSentFolder(client);
|
var sentFolder = client.GetFolder("已发送");
|
||||||
|
sentFolder.Open(FolderAccess.ReadOnly);
|
||||||
|
|
||||||
var startDate = maxTime ?? DateTime.MinValue;
|
var startDate = maxTime ?? DateTime.MinValue;
|
||||||
var searchQuery = SearchQuery.All.And(SearchQuery.DeliveredAfter(startDate));
|
var searchQuery = SearchQuery.All.And(SearchQuery.DeliveredAfter(startDate));
|
||||||
|
|
@ -332,111 +334,6 @@ public class EmailLogService(IRepository<EmailLog> _emailLogRepository,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IMailFolder OpenSentFolder(ImapClient client)
|
|
||||||
{
|
|
||||||
IMailFolder folder = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
folder = client.GetFolder(SpecialFolder.Sent);
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
|
|
||||||
if (folder == null)
|
|
||||||
{
|
|
||||||
var candidates = new[] { "已发送", "已发送邮件", "Sent", "Sent Items", "[Gmail]/Sent Mail" };
|
|
||||||
foreach (var name in candidates)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var f = client.GetFolder(name);
|
|
||||||
if (f != null)
|
|
||||||
{
|
|
||||||
folder = f;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (folder == null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var personal = client.GetFolder(client.PersonalNamespaces.FirstOrDefault());
|
|
||||||
if (personal != null)
|
|
||||||
{
|
|
||||||
foreach (var sub in personal.GetSubfolders(false))
|
|
||||||
{
|
|
||||||
if (string.Equals(sub.FullName, "Sent", StringComparison.OrdinalIgnoreCase) ||
|
|
||||||
string.Equals(sub.FullName, "Sent Items", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
folder = sub;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
|
|
||||||
if (folder == null)
|
|
||||||
throw new InvalidOperationException("未找到已发送文件夹");
|
|
||||||
|
|
||||||
folder.Open(FolderAccess.ReadOnly);
|
|
||||||
return folder;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AuthenticateImap(ImapClient client)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client.Authenticate(_systemEmailConfig.FromEmail, _systemEmailConfig.AuthorizationCode);
|
|
||||||
}
|
|
||||||
catch (AuthenticationException)
|
|
||||||
{
|
|
||||||
//if (_systemEmailConfig.UseOAuth2 && _systemEmailConfig.OAuth2AccessToken.IsNotNullOrEmpty())
|
|
||||||
//{
|
|
||||||
// var sasl = new SaslMechanismOAuth2(_systemEmailConfig.FromEmail, _systemEmailConfig.OAuth2AccessToken);
|
|
||||||
// client.Authenticate(sasl);
|
|
||||||
// return;
|
|
||||||
//}
|
|
||||||
//if (_systemEmailConfig.OAuth2AccessToken.IsNullOrEmpty())
|
|
||||||
//{
|
|
||||||
// var token = AcquireOAuth2TokenByPassword();
|
|
||||||
// if (token.IsNotNullOrEmpty())
|
|
||||||
// {
|
|
||||||
// _systemEmailConfig.OAuth2AccessToken = token;
|
|
||||||
// var sasl = new SaslMechanismOAuth2(_systemEmailConfig.FromEmail, token);
|
|
||||||
// client.Authenticate(sasl);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//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的值
|
// 取skip的值
|
||||||
public int CalcReverseSkip(int pageIndex, int pageSize, int totalCount)
|
public int CalcReverseSkip(int pageIndex, int pageSize, int totalCount)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue