From 36910cd4b4aaf2bb3d2d388721664b4357306e63 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Thu, 18 May 2023 13:39:10 +0400 Subject: [PATCH] Getting started with emails. --- .../BankYouBankruptBusinessLogic.csproj | 1 + .../MailWorker/AbstractMailWorker.cs | 107 ++++++++++++++++++ .../MailWorker/MailKitWorker.cs | 91 +++++++++++++++ .../BindingModels/MailConfigBindingModel.cs | 24 ++++ .../BankYouBankruptRestAPI/Program.cs | 15 +++ 5 files changed, 238 insertions(+) create mode 100644 BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 BankYouBankrupt/BankYouBankruptContracts/BindingModels/MailConfigBindingModel.cs diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/BankYouBankruptBusinessLogic.csproj b/BankYouBankrupt/BankYouBankruptBusinessLogic/BankYouBankruptBusinessLogic.csproj index 1fa554a..28e1abd 100644 --- a/BankYouBankrupt/BankYouBankruptBusinessLogic/BankYouBankruptBusinessLogic.csproj +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/BankYouBankruptBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/AbstractMailWorker.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..1efa243 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,107 @@ +using BankYouBankruptContracts.BindingModels; +using BankYouBankruptContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + + protected string _mailPassword = string.Empty; + + protected string _smtpClientHost = string.Empty; + + protected int _smtpClientPort; + + protected string _popHost = string.Empty; + + protected int _popPort; + + private readonly IMessageInfoLogic _messageInfoLogic; + + private readonly IClientLogic _clientLogic; + + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + _clientLogic = clientLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, { popHost}, { popPort}", + _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + + await SendMailAsync(info); + } + + public async void MailCheck() + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_popHost) || _popPort == 0) + { + return; + } + + if (_messageInfoLogic == null) + { + return; + } + + var list = await ReceiveMailAsync(); + + _logger.LogDebug("Check Mail: {Count} new mails", list.Count); + + foreach (var mail in list) + { + mail.ClientId = _clientLogic.ReadElement(new() { Email = mail.SenderName })?.Id; + + _messageInfoLogic.Create(mail); + } + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + + protected abstract Task> ReceiveMailAsync(); + } +} diff --git a/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/MailKitWorker.cs b/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..b946d3e --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,91 @@ +using BankYouBankruptContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace BankYouBankruptBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) + : base(logger, messageInfoLogic, clientLogic) { } + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + + protected override async Task> ReceiveMailAsync() + { + var list = new List(); + + using var client = new Pop3Client(); + + await Task.Run(() => + { + try + { + client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect); + + client.Authenticate(_mailLogin, _mailPassword); + + for (int i = 0; i < client.Count; i++) + { + var message = client.GetMessage(i); + + foreach (var mail in message.From.Mailboxes) + { + list.Add(new MessageInfoBindingModel + { + DateDelivery = message.Date.DateTime, + MessageId = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody + }); + } + } + } + catch (AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + + return list; + } + } +} diff --git a/BankYouBankrupt/BankYouBankruptContracts/BindingModels/MailConfigBindingModel.cs b/BankYouBankrupt/BankYouBankruptContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..a369088 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankYouBankruptContracts.BindingModels +{ + //один из двух классов для передачи данных по почте + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + + public string MailPassword { get; set; } = string.Empty; + + public string SmtpClientHost { get; set; } = string.Empty; + + public int SmtpClientPort { get; set; } + + public string PopHost { get; set; } = string.Empty; + + public int PopPort { get; set; } + } +} diff --git a/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs b/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs index c3c2cd3..1582a28 100644 --- a/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs +++ b/BankYouBankrupt/BankYouBankruptRestAPI/Program.cs @@ -1,6 +1,8 @@ using BankYouBankruptBusinessLogic.BusinessLogics; +using BankYouBankruptBusinessLogic.MailWorker; using BankYouBankruptBusinessLogic.OfficePackage; using BankYouBankruptBusinessLogic.OfficePackage.Implements; +using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.BusinessLogicsContracts; using BankYouBankruptContracts.StoragesContracts; using BankYouBankruptDatabaseImplement.Implements; @@ -54,6 +56,19 @@ builder.Services.AddSwaggerGen(c => var app = builder.Build(); +//Mails Service +var mailSender = app.Services.GetService(); + +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty, + SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty, + SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty, + PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {