From 9671f1552386dc204821cd426834e4c1f5bf5c14 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 21 Dec 2023 12:31:25 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Background/WorkToSendEmail.cs | 30 +++++++---- .../EmailNotificationTransportService.cs | 51 ++++--------------- 2 files changed, 32 insertions(+), 49 deletions(-) diff --git a/AsbCloudInfrastructure/Background/WorkToSendEmail.cs b/AsbCloudInfrastructure/Background/WorkToSendEmail.cs index da1f273f..06e88342 100644 --- a/AsbCloudInfrastructure/Background/WorkToSendEmail.cs +++ b/AsbCloudInfrastructure/Background/WorkToSendEmail.cs @@ -1,39 +1,51 @@ using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; -using AsbCloudApp.Services; -using AsbCloudDb.Model; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; using System.Net.Mail; -using System.Text; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Background { + /// + /// Класс для отправки email + /// internal class WorkToSendEmail : Work { private NotificationDto notification; private string sender; private string smtpPassword; private string smtpServer; + private bool IsConfigured; - public WorkToSendEmail(string workId, NotificationDto notification, string sender, string smtpPassword, string smtpServer) : base(workId) + public WorkToSendEmail(string workId, NotificationDto notification, IConfiguration configuration) : base(workId) { this.notification = notification; - this.sender = sender; - this.smtpPassword = smtpPassword; - this.smtpServer = smtpServer; + sender = configuration.GetValue("email:sender", string.Empty); + smtpPassword = configuration.GetValue("email:password", string.Empty); + smtpServer = configuration.GetValue("email:smtpServer", string.Empty); + + var configError = string.IsNullOrEmpty(sender) || + string.IsNullOrEmpty(smtpPassword) || + string.IsNullOrEmpty(smtpServer); + + IsConfigured = !configError; } + protected override async Task Action(string id, IServiceProvider services, Action onProgressCallback, CancellationToken token) { + if (!IsConfigured) + { + Trace.TraceWarning("smtp is not configured"); + return; + } + var notificationRepository = services.GetRequiredService(); var userRepository = services.GetRequiredService(); diff --git a/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs b/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs index e345c351..d24d150f 100644 --- a/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs +++ b/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs @@ -1,19 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Net.Mail; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data; -using AsbCloudApp.Exceptions; -using AsbCloudApp.Repositories; +using AsbCloudApp.Data; using AsbCloudApp.Services.Notifications; using AsbCloudInfrastructure.Background; -using AsbCloudInfrastructure.Background.PeriodicWorks; -using DocumentFormat.OpenXml.Presentation; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.Email { @@ -21,45 +13,24 @@ namespace AsbCloudInfrastructure.Services.Email public class EmailNotificationTransportService : INotificationTransportService { private readonly BackgroundWorker backgroundWorker; - private readonly bool IsConfigured; - private readonly string sender; - private readonly string smtpServer; - private readonly string smtpPassword; private IConfiguration configuration; - public EmailNotificationTransportService(BackgroundWorker backgroundWorker, + public EmailNotificationTransportService(BackgroundWorker backgroundWorker, IConfiguration configuration) { - sender = configuration.GetValue("email:sender", string.Empty); - smtpPassword = configuration.GetValue("email:password", string.Empty); - smtpServer = configuration.GetValue("email:smtpServer", string.Empty); - - - var configError = string.IsNullOrEmpty(sender) || - string.IsNullOrEmpty(smtpPassword) || - string.IsNullOrEmpty(smtpServer); - - IsConfigured = !configError; - + this.configuration = configuration; this.backgroundWorker = backgroundWorker; } public int IdTransportType => 1; - + public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken) { - //if (!IsConfigured) - //{ - // Trace.TraceWarning("smtp is not configured"); - // return Task.CompletedTask; - //} - var workId = MakeWorkId(notification.IdUser, notification.Title, notification.Message); - if (!backgroundWorker.Works.Any(w=>w.Id==workId)) + if (!backgroundWorker.Works.Any(w => w.Id == workId)) { - var work = new WorkToSendEmail(workId, notification, sender, smtpPassword, smtpServer); + var work = new WorkToSendEmail(workId, notification, configuration); backgroundWorker.Enqueue(work); - } return Task.CompletedTask; @@ -72,7 +43,7 @@ namespace AsbCloudInfrastructure.Services.Email return Task.WhenAll(tasks); } - + private static string MakeWorkId(int idUser, string subject, string content) { var hash = idUser.GetHashCode();