From 422b08deeece507d13832949706762f64cb3845d Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 30 Apr 2023 18:06:01 +0400 Subject: [PATCH] MailKitWorker added --- .../JewelryStoreBusinessLogic.csproj | 1 + .../MailWorker/MailKitWorker.cs | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs diff --git a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index c29c38b..6b32983 100644 --- a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -16,6 +16,7 @@ + diff --git a/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs b/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..1a6b3b9 --- /dev/null +++ b/JewelryStoreBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,78 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System.Net; +using System.Net.Mail; +using System.Text; + +namespace JewelryStoreBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + + 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; + } + } +}