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

122 lines
4.5 KiB
C#
Raw Normal View History

2022-02-28 14:44:26 +05:00
using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudInfrastructure.Background;
2022-02-28 14:44:26 +05:00
namespace AsbCloudInfrastructure.Services
{
2022-02-28 14:44:26 +05:00
public class EmailService : IEmailService
{
private readonly BackgroundWorker backgroundWorker;
2022-02-28 14:44:26 +05:00
private readonly bool IsConfigured;
private readonly string sender;
private readonly string smtpServer;
private readonly string smtpPassword;
public EmailService(BackgroundWorker backgroundWorker, IConfiguration configuration)
2022-02-28 14:44:26 +05:00
{
sender = configuration.GetValue("email:sender", string.Empty);
smtpPassword = configuration.GetValue("email:password", string.Empty);
smtpServer = configuration.GetValue("email:smtpServer", string.Empty);
2022-02-28 14:44:26 +05:00
var configError = string.IsNullOrEmpty(sender) ||
2022-02-28 14:44:26 +05:00
string.IsNullOrEmpty(smtpPassword) ||
string.IsNullOrEmpty(smtpServer);
2022-02-28 14:44:26 +05:00
IsConfigured = !configError;
this.backgroundWorker = backgroundWorker;
}
public void EnqueueSend(string address, string subject, string htmlBody)
2022-05-05 13:28:48 +05:00
=> EnqueueSend(new List<string> { address }, subject, htmlBody);
2022-02-28 14:44:26 +05:00
public void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody)
{
2022-04-11 18:00:34 +05:00
if (!IsConfigured)
2022-02-28 14:44:26 +05:00
{
Trace.TraceWarning("smtp is not configured");
return;
}
var workId = MakeWorkId(addresses, subject, htmlBody);
if (!backgroundWorker.Contains(workId))
2022-02-28 14:44:26 +05:00
{
var workAction = MakeEmailSendWorkAction(addresses, subject, htmlBody);
var work = new WorkBase(workId, workAction);
backgroundWorker.Push(work);
2022-04-11 18:00:34 +05:00
}
2022-02-28 14:44:26 +05:00
}
private Func<string, IServiceProvider, CancellationToken, Task> MakeEmailSendWorkAction(IEnumerable<string> addresses, string subject, string htmlBody)
2022-02-28 14:44:26 +05:00
{
var mailAddresses = new List<MailAddress>();
foreach (var address in addresses)
{
if (MailAddress.TryCreate(address, out MailAddress? mailAddress))
2022-02-28 14:44:26 +05:00
mailAddresses.Add(mailAddress);
else
Trace.TraceWarning($"Mail {address} is not correct.");
}
if (!mailAddresses.Any())
throw new ArgumentException($"No valid email found. List:[{string.Join(',', addresses)}]", nameof(addresses));
if (string.IsNullOrEmpty(subject))
throw new ArgumentInvalidException($"{nameof(subject)} should be set", nameof(subject));
2022-04-11 18:00:34 +05:00
var workAction = async (string id, IServiceProvider serviceProvider, CancellationToken token) =>
2022-02-28 14:44:26 +05:00
{
var from = new MailAddress(sender);
var message = new MailMessage
{
From = from
};
2022-02-28 14:44:26 +05:00
foreach (var mailAddress in mailAddresses)
message.To.Add(mailAddress);
2022-04-11 18:00:34 +05:00
2022-05-05 13:28:48 +05:00
message.BodyEncoding = System.Text.Encoding.UTF8;
2022-02-28 14:44:26 +05:00
message.Body = htmlBody;
2022-04-25 15:38:44 +05:00
message.Subject = subject;
2022-02-28 14:44:26 +05:00
message.IsBodyHtml = true;
2022-05-05 10:45:04 +05:00
using var client = new SmtpClient(smtpServer);
2022-02-28 14:44:26 +05:00
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
2022-04-11 18:00:34 +05:00
2022-05-05 13:28:48 +05:00
await client.SendMailAsync(message, token);
Trace.TraceInformation($"Send email to {string.Join(',', addresses)} subj:{subject} html body count {htmlBody.Length}");
2022-02-28 14:44:26 +05:00
};
return workAction;
2022-04-11 18:00:34 +05:00
}
2022-02-28 14:44:26 +05:00
private static string MakeWorkId(IEnumerable<string> addresses, string subject, string content)
2022-02-28 14:44:26 +05:00
{
var hash = GetHashCode(addresses);
hash ^= subject.GetHashCode();
hash ^= content.GetHashCode();
return hash.ToString("x");
}
private static int GetHashCode(IEnumerable<string> strings)
{
int hash = -1397075115;
var enumerator = strings.GetEnumerator();
2022-04-11 18:00:34 +05:00
while (enumerator.MoveNext())
2022-02-28 14:44:26 +05:00
hash ^= enumerator.Current.GetHashCode();
return hash;
}
}
2022-02-28 14:44:26 +05:00
}