This commit is contained in:
Mikhail Kuznetsov
2023-06-15 10:14:27 +10:00
parent ab7e88c0ff
commit 73a824057b
8 changed files with 67 additions and 27 deletions

View File

@@ -7,7 +7,7 @@ namespace PARR.Mail.Services
public interface IMailService
{
Task<IList<IMessageSummary>> CheckMailAsync(SearchQuery query);
Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary);
Task<MimeEntity?> GetBodyPartAsync(IMessageSummary messageSummary);
Task<List<MimeEntity>> GetAttachmentsAsync(IMessageSummary messageSummary);
Task<string> GetBodyTextAsync(IMessageSummary messageSummary);
Task SetAsSeenAsync(IMessageSummary messageSummary);

View File

@@ -35,50 +35,86 @@ namespace PARR.Mail.Services
return items;
}
public async Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary)
public async Task<MimeEntity?> GetBodyPartAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
try
{
using var client = await GetClient();
return await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody);
return await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody);
}
catch (Exception e)
{
logger.LogError(e, "Ошибка получения тела письма");
}
return null;
}
public async Task<List<MimeEntity>> GetAttachmentsAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
List<MimeEntity> result = new List<MimeEntity>();
foreach (var attachment in messageSummary.Attachments)
try
{
result.Add(await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, attachment));
using var client = await GetClient();
foreach (var attachment in messageSummary.Attachments)
{
result.Add(await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, attachment));
}
}
catch (Exception e)
{
logger.LogError(e, "Ошибка получения тела письма");
}
return result;
}
public async Task<string> GetBodyTextAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
return ((TextPart)await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody)).Text;
try
{
using var client = await GetClient();
return ((TextPart)await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody)).Text;
}
catch (Exception e)
{
logger.LogError(e, "Ошибка содержания письма");
}
return string.Empty;
}
public async Task SetAsSeenAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
try
{
using var client = await GetClient();
await client.Inbox.AddFlagsAsync(messageSummary.UniqueId, MessageFlags.Seen, false);
await client.Inbox.AddFlagsAsync(messageSummary.UniqueId, MessageFlags.Seen, false);
}
catch (Exception e)
{
logger.LogError(e, "Не удалось сделать письмо прочитанным");
}
}
private async Task<ImapClient> GetClient()
{
var client = new ImapClient();
await client.ConnectAsync(mailSettings.Host, mailSettings.Port, SecureSocketOptions.None);
await client.AuthenticateAsync(mailSettings.UserName, mailSettings.Password);
await client.Inbox.OpenAsync(FolderAccess.ReadWrite);
// игнорить сертификат
client.CheckCertificateRevocation = false;
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
try
{
await client.ConnectAsync(mailSettings.Host, mailSettings.Port, SecureSocketOptions.None);
await client.AuthenticateAsync(mailSettings.UserName, mailSettings.Password);
await client.Inbox.OpenAsync(FolderAccess.ReadWrite);
// игнорить сертификат
client.CheckCertificateRevocation = false;
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
}
catch(Exception e) {
logger.LogError(e, "Не удалось создать почтовый клиент");
}
return client;
}
}