From a5ebf1c7ad0d6693bd1161407700b4ae0299b510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Thu, 18 May 2023 22:25:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B0.=20=D0=A3=D0=A0=D0=90=D0=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MailKit/AbstractMailWorker.cs | 70 +++++++++++++++++++ .../MailKit/MailKitWorker.cs | 48 +++++++++++++ .../BindingModel/MailConfigBindingModel.cs | 23 ++++++ .../BindingModel/MailSendInfoBindingModel.cs | 17 +++++ .../SchoolAgainStudyContracts.csproj | 1 - SchoolAgainStudy/StudentWebClient/App.config | 11 +++ .../Controllers/HomeController.cs | 36 +++++++++- SchoolAgainStudy/StudentWebClient/Program.cs | 7 ++ .../StudentWebClient/StudentWebClient.csproj | 10 +++ 9 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/AbstractMailWorker.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/MailKitWorker.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailConfigBindingModel.cs create mode 100644 SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailSendInfoBindingModel.cs create mode 100644 SchoolAgainStudy/StudentWebClient/App.config diff --git a/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/AbstractMailWorker.cs b/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/AbstractMailWorker.cs new file mode 100644 index 0000000..16fe16f --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/AbstractMailWorker.cs @@ -0,0 +1,70 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyBusinessLogic.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 ILogger _logger; + + 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/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/MailKitWorker.cs b/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/MailKitWorker.cs new file mode 100644 index 0000000..73086b2 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/MailKit/MailKitWorker.cs @@ -0,0 +1,48 @@ +using SchoolAgainStudyContracts.BindingModel; +using SchoolAgainStudyContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System.Net; +using System.Net.Mail; +using System.Text; +using SchoolAgainStudyContracts.StorageContracts; +using SchoolAgainStudyContracts.SearchModel; + +namespace SchoolAgainStudyBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + + public MailKitWorker(ILogger logger, IStudentStorage clientStorage) : 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.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; + } + } + + + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailConfigBindingModel.cs b/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailConfigBindingModel.cs new file mode 100644 index 0000000..5d6c940 --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailConfigBindingModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyContracts.BindingModel +{ + 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/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailSendInfoBindingModel.cs b/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..86485db --- /dev/null +++ b/SchoolAgainStudy/SchoolAgainStudyContracts/BindingModel/MailSendInfoBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SchoolAgainStudyContracts.BindingModel +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + + public string Subject { get; set; } = string.Empty; + + public string Path { get; set; } = string.Empty; + } +} diff --git a/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj b/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj index 004cf8d..28b2e82 100644 --- a/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj +++ b/SchoolAgainStudy/SchoolAgainStudyContracts/SchoolAgainStudyContracts.csproj @@ -11,7 +11,6 @@ - diff --git a/SchoolAgainStudy/StudentWebClient/App.config b/SchoolAgainStudy/StudentWebClient/App.config new file mode 100644 index 0000000..6c999e7 --- /dev/null +++ b/SchoolAgainStudy/StudentWebClient/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs b/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs index 2bb2bcf..08edbe4 100644 --- a/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs +++ b/SchoolAgainStudy/StudentWebClient/Controllers/HomeController.cs @@ -1,5 +1,7 @@ -using Microsoft.AspNetCore.Mvc; +using DocumentFormat.OpenXml.Wordprocessing; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; +using SchoolAgainStudyBusinessLogic.MailWorker; using SchoolAgainStudyContracts.BindingModel; using SchoolAgainStudyContracts.BusinessLogicContracts; using SchoolAgainStudyContracts.SearchModel; @@ -21,8 +23,9 @@ namespace StudentWebClient.Controllers private readonly IInterestLogic _interest; private readonly IStudentLogic _student; private readonly IReportLogic _report; + private readonly AbstractMailWorker mailSender; public HomeController(ILogger logger, IDiyLogic diy, - IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student, IReportLogic report) + IProductLogic product , ITaskLogic task , IInterestLogic interest, IStudentLogic student, IReportLogic report, AbstractMailWorker abstractMailWorker) { _logger = logger; _diy = diy; @@ -31,6 +34,25 @@ namespace StudentWebClient.Controllers _interest = interest; _student = student; _report = report; + + try + { + mailSender = abstractMailWorker; + mailSender?.MailConfig(new MailConfigBindingModel + { + MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, + MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty, + SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty, + SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]), + PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty, + PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"]) + }); + + } + catch (Exception ex) + { + logger?.LogError(ex, "Ошибка работы с почтой"); + } } public IActionResult Index() @@ -142,6 +164,7 @@ namespace StudentWebClient.Controllers [HttpGet] public IActionResult CreateInterest() { + return View(); } [HttpPost] @@ -461,11 +484,18 @@ namespace StudentWebClient.Controllers { throw new Exception("Не верные даты"); } + string path = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name} от {_dateFrom.ToString("dd/MM/yyyy")}.pdf"; _report.SaveInterestsToPdfFile - (new ReportBindingModel { FileName = $"C:\\Users\\Вова\\Documents\\Учеба\\Reports\\{APIClient.Student.Name} от {_dateFrom.ToString("dd/MM/yyyy")}.pdf" , + (new ReportBindingModel { FileName = path, DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id}); var items = _report.GetInterests(new ReportBindingModel { DateFrom = _dateFrom, DateTo = _dateTo, StudentId = APIClient.Student.Id }); + mailSender.MailSendAsync(new MailSendInfoBindingModel + { + MailAddress = APIClient.Student.Email, + Subject = "Заказ номер", + Path = path + }); return PartialView("_LessonListPartial", items); } } diff --git a/SchoolAgainStudy/StudentWebClient/Program.cs b/SchoolAgainStudy/StudentWebClient/Program.cs index e9305d6..756597f 100644 --- a/SchoolAgainStudy/StudentWebClient/Program.cs +++ b/SchoolAgainStudy/StudentWebClient/Program.cs @@ -6,10 +6,14 @@ using SchoolAgainStudyContracts.BusinessLogicContracts; using SchoolAgainStudyContracts.StorageContracts; using SchoolAgainStudyDataBaseImplements.Implements; using StudentWebClient; +using SchoolAgainStudyBusinessLogic.MailWorker; +using SchoolAgainStudyContracts.BindingModel; var builder = WebApplication.CreateBuilder(args); + builder.Logging.SetMinimumLevel(LogLevel.Trace); builder.Logging.AddLog4Net("log4net.config"); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); @@ -29,12 +33,15 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddSingleton(); + builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); + // Add services to the container. builder.Services.AddControllersWithViews(); var app = builder.Build(); diff --git a/SchoolAgainStudy/StudentWebClient/StudentWebClient.csproj b/SchoolAgainStudy/StudentWebClient/StudentWebClient.csproj index 8be91af..dad113c 100644 --- a/SchoolAgainStudy/StudentWebClient/StudentWebClient.csproj +++ b/SchoolAgainStudy/StudentWebClient/StudentWebClient.csproj @@ -6,6 +6,16 @@ enable + + + + + + + Always + + + all