Syncer АИХ ИТ

This commit is contained in:
Mikhail Kuznetsov
2023-06-01 12:30:38 +10:00
parent e34037267c
commit 118cd280df
27 changed files with 953 additions and 8 deletions

View File

@@ -0,0 +1,19 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PARR.Mail.Services;
using PARR.Mail.Settings;
namespace PARR.Mail
{
public static class MailInstaller
{
public static void InstallMailServices(this IServiceCollection services, IConfiguration configuration)
{
var mailSettings = new MailSettings();
configuration.GetSection(nameof(MailSettings)).Bind(mailSettings);
services.AddSingleton(mailSettings);
services.AddTransient<IMailService, MailService>();
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="4.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog" Version="2.12.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,15 @@
using MailKit;
using MailKit.Search;
using MimeKit;
namespace PARR.Mail.Services
{
public interface IMailService
{
Task<IList<IMessageSummary>> CheckMailAsync(SearchQuery query);
Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary);
Task<List<MimeEntity>> GetAttachmentsAsync(IMessageSummary messageSummary);
Task<string> GetBodyTextAsync(IMessageSummary messageSummary);
Task SetAsSeenAsync(IMessageSummary messageSummary);
}
}

View File

@@ -0,0 +1,85 @@
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using MimeKit;
using PARR.Mail.Settings;
namespace PARR.Mail.Services
{
internal class MailService : IMailService
{
private readonly ILogger<MailService> logger;
private readonly MailSettings mailSettings;
public MailService(ILogger<MailService> logger, MailSettings mailSettings)
{
this.logger = logger;
this.mailSettings = mailSettings;
}
public async Task<IList<IMessageSummary>> CheckMailAsync(SearchQuery query)
{
using var client = await GetClient();
IList<IMessageSummary> items = new List<IMessageSummary>();
try
{
var uids = await client.Inbox.SearchAsync(query);
items = await client.Inbox.FetchAsync(uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope);
}
catch (Exception e)
{
logger.LogError(e, "Ошибка получения списка писем");
}
return items;
}
public async Task<MimeEntity> GetBodyPartAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
return await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody);
}
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)
{
result.Add(await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, attachment));
}
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;
}
public async Task SetAsSeenAsync(IMessageSummary messageSummary)
{
using var client = await GetClient();
await client.Inbox.AddFlagsAsync(messageSummary.UniqueId, MessageFlags.Seen, false);
}
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;
return client;
}
}
}

View File

@@ -0,0 +1,11 @@
namespace PARR.Mail.Settings
{
internal class MailSettings
{
public string Host { get; set; }
public int Port { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public bool SSL { get; set; }
}
}