From c5b4a83d785f49370906f994f45e064a0fa9b59e Mon Sep 17 00:00:00 2001 From: Allllen4a Date: Sun, 2 Jun 2024 14:25:04 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=82=D1=80=D0=BE=20=D1=80=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=B2=D0=BB=D1=8F=D0=B5=D1=82=20=D0=BF=D0=BE?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=81=D1=82=D0=B0=D0=BC=20=D0=B2=D1=81=D0=B5,?= =?UTF-8?q?=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20=D0=B2=D0=B5=D1=89=D0=B5?= =?UTF-8?q?=D0=B9=20=D0=BD=D0=B0=20=D0=B6=D1=83=D1=80=D0=BD=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=BE=D0=BC=20=D1=81=D1=82=D0=BE=D0=BB=D0=B8=D0=BA?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MailWorker/AbstractMailWorker.cs | 153 ++++++++++-------- .../MailWorker/MailKitWorker.cs | 105 +++++------- 2 files changed, 127 insertions(+), 131 deletions(-) diff --git a/BeautySalonView/BeautySalonBusinesLogic/MailWorker/AbstractMailWorker.cs b/BeautySalonView/BeautySalonBusinesLogic/MailWorker/AbstractMailWorker.cs index d0457c4..0fd3401 100644 --- a/BeautySalonView/BeautySalonBusinesLogic/MailWorker/AbstractMailWorker.cs +++ b/BeautySalonView/BeautySalonBusinesLogic/MailWorker/AbstractMailWorker.cs @@ -9,77 +9,98 @@ using BeautySalonContracts.BusinessLogicsContracts; namespace BeautySalonBusinesLogic.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 ILogger _logger; + public abstract class AbstractMailWorker + { + /// + /// Логгер + /// + private readonly ILogger _logger; - public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) - { - _logger = logger; - _messageInfoLogic = messageInfoLogic; - } - 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.Length, _smtpClientHost, _smtpClientPort, _popHost, _popPort); - } - public async void MailSendAsync(MailSendInfoBindingModel info) - { - if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) - { - return; - } + /// + /// Логин для доступа к почтовому сервису + /// + protected string _mailLogin = string.Empty; - if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) - { - return; - } + /// + /// Пароль для доступа к почтовому сервису + /// + protected string _mailPassword = string.Empty; - if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) - { - return; - } + /// + /// Хост SMTP-клиента + /// + protected string _smtpClientHost = string.Empty; - _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; - } + /// + /// Порт SMTP-клиента + /// + protected int _smtpClientPort; - if (string.IsNullOrEmpty(_popHost) || _popPort == 0) - { - return; - } + /// + /// Хост протокола POP3 + /// + protected string _popHost = string.Empty; - if (_messageInfoLogic == null) - { - return; - } + /// + /// Порт протокола POP3 + /// + protected int _popPort; - var list = await ReceiveMailAsync(); - _logger.LogDebug("Check Mail: {Count} new mails", list.Count); - foreach (var mail in list) - { - _messageInfoLogic.Create(mail); - } - } - protected abstract Task SendMailAsync(MailSendInfoBindingModel info); - protected abstract Task> ReceiveMailAsync(); - } + /// + /// Конструктор + /// + /// + public AbstractMailWorker(ILogger logger) + { + _logger = logger; + } + + /// + /// Настроить почтовый сервис + /// + /// + 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.Path)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + await SendMailAsync(info); + } + + /// + /// Отправить письмо + /// + /// + /// + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + } } diff --git a/BeautySalonView/BeautySalonBusinesLogic/MailWorker/MailKitWorker.cs b/BeautySalonView/BeautySalonBusinesLogic/MailWorker/MailKitWorker.cs index 011f74c..5b73096 100644 --- a/BeautySalonView/BeautySalonBusinesLogic/MailWorker/MailKitWorker.cs +++ b/BeautySalonView/BeautySalonBusinesLogic/MailWorker/MailKitWorker.cs @@ -13,70 +13,45 @@ using BeautySalonContracts.BusinessLogicsContracts; namespace BeautySalonBusinesLogic.MailWorker { - public class MailKitWorker : AbstractMailWorker - { - public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + public class MailKitWorker : AbstractMailWorker + { + /// + /// Конструктор + /// + /// + public MailKitWorker(ILogger logger) : base(logger) { } - 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 (MailKit.Security.AuthenticationException) - { } - finally - { - client.Disconnect(true); - } - }); - return list; - } - } + /// + /// Отправить письмо + /// + /// + /// + 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.Attachments.Add(new Attachment(info.Path)); + // Указываем параметры + 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; + } + } + } }