DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs

96 lines
3.3 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
2023-12-21 12:31:25 +05:00
using AsbCloudApp.Services.Notifications;
using Microsoft.Extensions.Configuration;
2022-02-28 14:44:26 +05:00
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
2022-02-28 14:44:26 +05:00
using System.Linq;
using System.Net.Mail;
2022-02-28 14:44:26 +05:00
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure.Services.Email;
public class EmailNotificationTransportService : INotificationTransportService
2022-02-28 14:44:26 +05:00
{
2024-08-19 10:01:07 +05:00
private readonly IUserRepository userRepository;
private readonly string sender;
private readonly string smtpPassword;
private readonly string smtpServer;
2024-08-19 10:01:07 +05:00
public int IdTransportType => 1;
public bool IsConfigured { get; }
2024-08-19 10:01:07 +05:00
public EmailNotificationTransportService(
IConfiguration configuration,
IUserRepository userRepository)
{
this.userRepository = userRepository;
2022-02-28 14:44:26 +05:00
2024-08-19 10:01:07 +05:00
this.sender = configuration.GetValue<string>("email:sender")
?? throw new ConfigurationErrorsException("email:sender не определен");
2022-02-28 14:44:26 +05:00
2024-08-19 10:01:07 +05:00
this.smtpPassword = configuration.GetValue<string>("email:password")
?? throw new ConfigurationErrorsException("email:password не определен");
2024-08-19 10:01:07 +05:00
this.smtpServer = configuration.GetValue<string>("email:smtpServer")
?? throw new ConfigurationErrorsException("email:smtpServer не определен");
2024-08-19 10:01:07 +05:00
var configError = string.IsNullOrEmpty(this.sender) ||
string.IsNullOrEmpty(this.smtpPassword) ||
string.IsNullOrEmpty(this.smtpServer);
2024-08-19 10:01:07 +05:00
this.IsConfigured = !configError;
}
2023-12-21 12:31:25 +05:00
2024-08-19 10:01:07 +05:00
public async Task SendAsync(NotificationDto notification, CancellationToken token)
{
if (!IsConfigured)
{
Trace.TraceWarning("smtp is not configured");
return;
}
2024-08-19 10:01:07 +05:00
var user = await userRepository.GetOrDefaultAsync(notification.IdUser, token)
?? throw new ArgumentInvalidException(nameof(notification.IdUser), "Пользователь не найден");
if (!MailAddress.TryCreate(user.Email, out var mailAddress))
2022-02-28 14:44:26 +05:00
{
2024-08-19 10:01:07 +05:00
Trace.TraceWarning($"Mail {user.Email} is not correct.");
throw new ArgumentInvalidException(nameof(user.Email), $"Mail {user.Email} is not null.");
}
2024-08-19 10:01:07 +05:00
var from = new MailAddress(sender);
var message = new MailMessage
{
2024-08-19 10:01:07 +05:00
From = from
};
2024-08-19 10:01:07 +05:00
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);
Trace.TraceInformation($"Send email to {user.Email} subj:{notification.Title} html body count {notification.Message.Length}");
}
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
{
var tasks = notifications
.Select(notification => SendAsync(notification, cancellationToken));
return Task.WhenAll(tasks);
2022-02-28 14:44:26 +05:00
}
}