From 47c4929ade881ebacb22c5552dec63b81f66fc85 Mon Sep 17 00:00:00 2001 From: the Date: Wed, 24 May 2023 21:41:02 +0400 Subject: [PATCH] email --- .../MailWorker/AbstractMailWorker.cs | 64 +++++++++++++++++++ .../MailWorker/MailKitWorker.cs | 49 ++++++++++++++ .../Controllers/HomeController.cs | 20 +++--- .../Views/Home/ReportPdf.cshtml | 2 +- .../BindingModels/MailConfigBindingModel.cs | 18 ++++++ .../BindingModels/MailSendInfoBindingModel.cs | 15 +++++ .../Controllers/ReportController.cs | 18 +++++- .../ComputerShopRestApi/Program.cs | 16 +++++ .../ComputerShopRestApi/appsettings.json | 9 ++- 9 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 ComputerShopProvider/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 ComputerShopProvider/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs b/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..29a01c7 --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,64 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.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 IClientLogic _clientLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IClientLogic clientLogic) + { + _logger = logger; + _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); + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + } +} diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs b/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..fdd408e --- /dev/null +++ b/ComputerShopProvider/ComputerShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,49 @@ +using ComputerShopContracts.BindingModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net.Mime; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using ComputerShopContracts.BusinessLogicContracts; + +namespace ComputerShopBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IClientLogic clientLogic) : base(logger, 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; + Attachment attachment = new Attachment("F:\\ReportsCourseWork\\pdffile.pdf", new ContentType(MediaTypeNames.Application.Pdf)); + objMailMessage.Attachments.Add(attachment); + + 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/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs index 14ad97f..a298cf0 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs +++ b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs @@ -226,21 +226,21 @@ namespace ComputerShopClientApp.Controllers { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } - //if (string.IsNullOrEmpty(clientEmail)) - //{ - // throw new Exception("Email пуст"); - //} + if (string.IsNullOrEmpty(clientEmail)) + { + throw new Exception("Email пуст"); + } APIClient.PostRequest("api/report/CreatePdfReport", new ReportBindingModel { DateFrom = dateFrom, DateTo = dateTo, }); - //APIClient.PostRequest("api/report/SendPdfToMail", new MailSendInfoBindingModel - //{ - // MailAddress = organiserEmail, - // Subject = "Отчет по участникам (pdf)", - // Text = "Отчет по участникам с " + dateFrom.ToShortDateString() + " до " + dateTo.ToShortDateString() - //}); + APIClient.PostRequest("api/report/SendPdfToMail", new MailSendInfoBindingModel + { + MailAddress = clientEmail, + Subject = "Отчет по закупкам (pdf)", + Text = "Отчет по закупкам с " + dateFrom.ToShortDateString() + " до " + dateTo.ToShortDateString() + }); Response.Redirect("GetPdfFile"); } diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/ReportPdf.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/ReportPdf.cshtml index 58e7bdc..867cf69 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Views/Home/ReportPdf.cshtml +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/ReportPdf.cshtml @@ -32,7 +32,7 @@
diff --git a/ComputerShopProvider/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs b/ComputerShopProvider/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..f82ebfe --- /dev/null +++ b/ComputerShopProvider/ComputerShopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.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/ComputerShopProvider/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs b/ComputerShopProvider/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..d949069 --- /dev/null +++ b/ComputerShopProvider/ComputerShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/ComputerShopProvider/ComputerShopRestApi/Controllers/ReportController.cs b/ComputerShopProvider/ComputerShopRestApi/Controllers/ReportController.cs index e8fa6a2..146da4d 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Controllers/ReportController.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Controllers/ReportController.cs @@ -4,6 +4,7 @@ using ComputerShopContracts.ViewModels; using ComputerShopBusinessLogic.BusinessLogics; using Microsoft.AspNetCore.Mvc; using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopBusinessLogic.MailWorker; [Route("api/[controller]/[action]")] [ApiController] @@ -11,10 +12,12 @@ public class ReportController : Controller { private readonly ILogger _logger; private readonly IReportLogic _reportLogic; - public ReportController(ILogger logger, IReportLogic reportLogic) + private readonly AbstractMailWorker _mailWorker; + public ReportController(ILogger logger, IReportLogic reportLogic, AbstractMailWorker mailWorker) { _logger = logger; _reportLogic = reportLogic; + _mailWorker = mailWorker; } [HttpPost] @@ -61,4 +64,17 @@ public class ReportController : Controller throw; } } + [HttpPost] + public void SendPdfToMail(MailSendInfoBindingModel model) + { + try + { + _mailWorker.MailSendAsync(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отправки письма"); + throw; + } + } } \ No newline at end of file diff --git a/ComputerShopProvider/ComputerShopRestApi/Program.cs b/ComputerShopProvider/ComputerShopRestApi/Program.cs index e0713e6..fefd536 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Program.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Program.cs @@ -1,6 +1,8 @@ using ComputerShopBusinessLogic.BusinessLogics; +using ComputerShopBusinessLogic.MailWorker; using ComputerShopBusinessLogic.OfficePackage; using ComputerShopBusinessLogic.OfficePackage.Implements; +using ComputerShopContracts.BindingModels; using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.StorageContracts; using ComputerShopDatabaseImplement.Implements; @@ -31,6 +33,8 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddSingleton(); + builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -46,6 +50,18 @@ builder.Services.AddSwaggerGen(c => var app = builder.Build(); +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()) { diff --git a/ComputerShopProvider/ComputerShopRestApi/appsettings.json b/ComputerShopProvider/ComputerShopRestApi/appsettings.json index 10f68b8..0689ca3 100644 --- a/ComputerShopProvider/ComputerShopRestApi/appsettings.json +++ b/ComputerShopProvider/ComputerShopRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "ulstulabs@gmail.com", + "MailPassword": "ymes wvnq jahv ulco" }