forked from ddrilling/AsbCloudServer
Рефакторинг WorkToSendEmail, перенос содержимого из метода Action в метод SendAsync сервиса EmailNotificationTransportService
This commit is contained in:
parent
2e8923b556
commit
f415406911
@ -1,11 +1,7 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using AsbCloudApp.Exceptions;
|
using AsbCloudApp.Services.Notifications;
|
||||||
using AsbCloudApp.Repositories;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Net.Mail;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -17,69 +13,16 @@ namespace AsbCloudInfrastructure.Background
|
|||||||
internal class WorkToSendEmail : Work
|
internal class WorkToSendEmail : Work
|
||||||
{
|
{
|
||||||
private NotificationDto notification;
|
private NotificationDto notification;
|
||||||
private string sender;
|
|
||||||
private string smtpPassword;
|
|
||||||
private string smtpServer;
|
|
||||||
private bool IsConfigured;
|
|
||||||
|
|
||||||
public WorkToSendEmail(NotificationDto notification, IConfiguration configuration) : base(MakeWorkId(notification))
|
public WorkToSendEmail(NotificationDto notification) : base(MakeWorkId(notification))
|
||||||
{
|
{
|
||||||
this.notification = notification;
|
this.notification = notification;
|
||||||
|
|
||||||
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<string, double?> onProgressCallback, CancellationToken token)
|
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!IsConfigured)
|
var notificationService = services.GetRequiredService<INotificationTransportService>();
|
||||||
{
|
await notificationService.SendAsync(notification, token);
|
||||||
Trace.TraceWarning("smtp is not configured");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var notificationRepository = services.GetRequiredService<INotificationRepository>();
|
|
||||||
var userRepository = services.GetRequiredService<IUserRepository>();
|
|
||||||
|
|
||||||
var user = await userRepository.GetOrDefaultAsync(notification.IdUser, token)
|
|
||||||
?? throw new ArgumentInvalidException(nameof(notification.IdUser), "Пользователь не найден");
|
|
||||||
|
|
||||||
if (!MailAddress.TryCreate(user.Email, out var mailAddress))
|
|
||||||
{
|
|
||||||
Trace.TraceWarning($"Mail {user.Email} is not correct.");
|
|
||||||
throw new ArgumentInvalidException(nameof(user.Email), $"Mail {user.Email} is not null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var from = new MailAddress(sender);
|
|
||||||
var message = new MailMessage
|
|
||||||
{
|
|
||||||
From = from
|
|
||||||
};
|
|
||||||
|
|
||||||
message.To.Add(mailAddress.Address);
|
|
||||||
message.BodyEncoding = System.Text.Encoding.UTF8;
|
|
||||||
message.Body = notification.Message;
|
|
||||||
message.Subject = notification.Title;
|
|
||||||
message.IsBodyHtml = true;
|
|
||||||
|
|
||||||
using var client = new SmtpClient(smtpServer);
|
|
||||||
client.EnableSsl = true;
|
|
||||||
client.UseDefaultCredentials = false;
|
|
||||||
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
|
|
||||||
|
|
||||||
await client.SendMailAsync(message, token);
|
|
||||||
notification.SentDate = DateTime.UtcNow;
|
|
||||||
await notificationRepository.UpdateAsync(notification, token);
|
|
||||||
|
|
||||||
Trace.TraceInformation($"Send email to {user.Email} subj:{notification.Title} html body count {notification.Message.Length}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using AsbCloudApp.Exceptions;
|
||||||
|
using AsbCloudApp.Repositories;
|
||||||
using AsbCloudApp.Services.Notifications;
|
using AsbCloudApp.Services.Notifications;
|
||||||
using AsbCloudInfrastructure.Background;
|
using AsbCloudInfrastructure.Background;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Mail;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -12,27 +17,73 @@ namespace AsbCloudInfrastructure.Services.Email
|
|||||||
|
|
||||||
public class EmailNotificationTransportService : INotificationTransportService
|
public class EmailNotificationTransportService : INotificationTransportService
|
||||||
{
|
{
|
||||||
private readonly IConfiguration configuration;
|
private readonly INotificationRepository notificationRepository;
|
||||||
private readonly BackgroundWorker backgroundWorker;
|
private readonly IUserRepository userRepository;
|
||||||
|
private readonly string sender;
|
||||||
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
private readonly string smtpPassword;
|
||||||
IConfiguration configuration)
|
private readonly string smtpServer;
|
||||||
{
|
|
||||||
this.configuration = configuration;
|
|
||||||
this.backgroundWorker = backgroundWorker;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int IdTransportType => 1;
|
public int IdTransportType => 1;
|
||||||
|
public bool IsConfigured { get; }
|
||||||
|
|
||||||
public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken)
|
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
||||||
|
IConfiguration configuration,
|
||||||
|
INotificationRepository notificationRepository,
|
||||||
|
IUserRepository userRepository)
|
||||||
{
|
{
|
||||||
var work = new WorkToSendEmail(notification, configuration);
|
this.notificationRepository = notificationRepository;
|
||||||
if (!backgroundWorker.Works.Any(w => w.Id == work.Id))
|
this.userRepository = userRepository;
|
||||||
|
|
||||||
|
this.sender = configuration.GetValue("email:sender", string.Empty);
|
||||||
|
this.smtpPassword = configuration.GetValue("email:password", string.Empty);
|
||||||
|
this.smtpServer = configuration.GetValue("email:smtpServer", string.Empty);
|
||||||
|
|
||||||
|
var configError = string.IsNullOrEmpty(this.sender) ||
|
||||||
|
string.IsNullOrEmpty(this.smtpPassword) ||
|
||||||
|
string.IsNullOrEmpty(this.smtpServer);
|
||||||
|
|
||||||
|
this.IsConfigured = !configError;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendAsync(NotificationDto notification, CancellationToken token)
|
||||||
|
{
|
||||||
|
if (!IsConfigured)
|
||||||
{
|
{
|
||||||
backgroundWorker.Enqueue(work);
|
Trace.TraceWarning("smtp is not configured");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
var user = await userRepository.GetOrDefaultAsync(notification.IdUser, token)
|
||||||
|
?? throw new ArgumentInvalidException(nameof(notification.IdUser), "Пользователь не найден");
|
||||||
|
|
||||||
|
if (!MailAddress.TryCreate(user.Email, out var mailAddress))
|
||||||
|
{
|
||||||
|
Trace.TraceWarning($"Mail {user.Email} is not correct.");
|
||||||
|
throw new ArgumentInvalidException(nameof(user.Email), $"Mail {user.Email} is not null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var from = new MailAddress(sender);
|
||||||
|
var message = new MailMessage
|
||||||
|
{
|
||||||
|
From = from
|
||||||
|
};
|
||||||
|
|
||||||
|
message.To.Add(mailAddress.Address);
|
||||||
|
message.BodyEncoding = System.Text.Encoding.UTF8;
|
||||||
|
message.Body = notification.Message;
|
||||||
|
message.Subject = notification.Title;
|
||||||
|
message.IsBodyHtml = true;
|
||||||
|
|
||||||
|
using var client = new SmtpClient(smtpServer);
|
||||||
|
client.EnableSsl = true;
|
||||||
|
client.UseDefaultCredentials = false;
|
||||||
|
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
|
||||||
|
|
||||||
|
await client.SendMailAsync(message, token);
|
||||||
|
notification.SentDate = DateTime.UtcNow;
|
||||||
|
await notificationRepository.UpdateAsync(notification, token);
|
||||||
|
|
||||||
|
Trace.TraceInformation($"Send email to {user.Email} subj:{notification.Title} html body count {notification.Message.Length}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
|
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
|
||||||
@ -42,7 +93,5 @@ namespace AsbCloudInfrastructure.Services.Email
|
|||||||
|
|
||||||
return Task.WhenAll(tasks);
|
return Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user