Worker
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user