forked from ddrilling/AsbCloudServer
Add emailService
This commit is contained in:
parent
f286410eff
commit
45cce481ee
10
AsbCloudApp/Services/IEmailService.cs
Normal file
10
AsbCloudApp/Services/IEmailService.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AsbCloudApp.Services
|
||||
{
|
||||
public interface IEmailService
|
||||
{
|
||||
void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody);
|
||||
}
|
||||
}
|
@ -50,6 +50,7 @@ namespace AsbCloudInfrastructure
|
||||
|
||||
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
|
||||
services.AddScoped<IFileShareService, GoogleDriveService>();
|
||||
services.AddScoped<IEmailService, EmailService>();
|
||||
|
||||
services.AddHostedService<TelemetryAnalyticsBackgroundService>();// replace by BackgroundWorkerService
|
||||
|
||||
|
@ -136,7 +136,8 @@ namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
await work.ActionAsync(work.Id, cts.Token).ConfigureAwait(false);
|
||||
var actionTask = work.ActionAsync(work.Id, cts.Token);
|
||||
await actionTask.WaitAsync(TimeSpan.FromMinutes(2), cts.Token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
115
AsbCloudInfrastructure/Services/EmailService.cs
Normal file
115
AsbCloudInfrastructure/Services/EmailService.cs
Normal file
@ -0,0 +1,115 @@
|
||||
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.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public class EmailService : IEmailService
|
||||
{
|
||||
private readonly IBackgroundWorkerService backgroundWorker;
|
||||
private readonly bool IsConfigured;
|
||||
private readonly string sender;
|
||||
private readonly string smtpServer;
|
||||
private readonly string smtpPassword;
|
||||
|
||||
public EmailService(IBackgroundWorkerService backgroundWorker, IConfiguration configuration)
|
||||
{
|
||||
sender = configuration.GetValue<string>("email:sender", null);
|
||||
smtpPassword = configuration.GetValue<string>("email:password", null);
|
||||
smtpServer = configuration.GetValue<string>("email:smtpServer", null);
|
||||
|
||||
var configError = (string.IsNullOrEmpty(sender) ||
|
||||
string.IsNullOrEmpty(smtpPassword) ||
|
||||
string.IsNullOrEmpty(smtpServer));
|
||||
|
||||
IsConfigured = !configError;
|
||||
|
||||
this.backgroundWorker = backgroundWorker;
|
||||
}
|
||||
|
||||
public void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody)
|
||||
{
|
||||
if(!IsConfigured)
|
||||
{
|
||||
Trace.TraceWarning("smtp is not configured");
|
||||
return;
|
||||
}
|
||||
var jobId = CalcJobId(addresses, subject, htmlBody);
|
||||
if (!backgroundWorker.Contains(jobId))
|
||||
{
|
||||
var action = MakeEmailSendJobAsync(addresses, subject, htmlBody);
|
||||
backgroundWorker.Enqueue(jobId, action);
|
||||
}
|
||||
}
|
||||
|
||||
private Func<string, CancellationToken, Task> MakeEmailSendJobAsync(IEnumerable<string> addresses, string subject, string htmlBody)
|
||||
{
|
||||
var mailAddresses = new List<MailAddress>();
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
if (MailAddress.TryCreate(address, out MailAddress mailAddress))
|
||||
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));
|
||||
|
||||
var func = async (string id, CancellationToken token) =>
|
||||
{
|
||||
var from = new MailAddress(sender);
|
||||
|
||||
var message = new MailMessage();
|
||||
message.From = from;
|
||||
|
||||
foreach (var mailAddress in mailAddresses)
|
||||
message.To.Add(mailAddress);
|
||||
|
||||
message.Subject = subject;
|
||||
message.Body = htmlBody;
|
||||
message.IsBodyHtml = true;
|
||||
|
||||
var client = new SmtpClient(smtpServer);
|
||||
client.EnableSsl = true;
|
||||
client.UseDefaultCredentials = false;
|
||||
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
|
||||
|
||||
// TODO: uncomment next when tested
|
||||
//await client.SendMailAsync(message, token);
|
||||
await Task.Delay(0);
|
||||
Trace.TraceInformation($"Send email to {string.Join(',', addresses)} subj:{subject}");
|
||||
};
|
||||
return func;
|
||||
}
|
||||
|
||||
private string CalcJobId(IEnumerable<string> addresses, string subject, string content)
|
||||
{
|
||||
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();
|
||||
|
||||
while(enumerator.MoveNext())
|
||||
hash ^= enumerator.Current.GetHashCode();
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
}
|
BIN
AsbCloudWebApi/Docs/Mail Templates.odt
Normal file
BIN
AsbCloudWebApi/Docs/Mail Templates.odt
Normal file
Binary file not shown.
@ -13,7 +13,12 @@
|
||||
"LocalConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Urls": "http://0.0.0.0:5000",//;https://0.0.0.0:5001" //,
|
||||
"email": {
|
||||
"smtpServer": "smtp.timeweb.ru",
|
||||
"sender": "bot@autodrilling.ru",
|
||||
"password": "xHhgwZ4D"
|
||||
},
|
||||
"Urls": "http://0.0.0.0:5000" //;https://0.0.0.0:5001" //,
|
||||
// See https man: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0
|
||||
//"Kestrel": {
|
||||
// "Endpoints": {
|
||||
|
@ -8,6 +8,7 @@ using AsbCloudDb;
|
||||
using Google.Apis.Drive.v3.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Net.Mail;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ConsoleApp1
|
||||
{
|
||||
@ -18,9 +19,14 @@ namespace ConsoleApp1
|
||||
|
||||
class Program
|
||||
{
|
||||
|
||||
|
||||
static void Main(/*string[] args*/)
|
||||
{
|
||||
SendMail.Main();
|
||||
var regex = new Regex(@"<[a-zA-Z0-9]+.*>.*<[a-zA-Z]+/*>");
|
||||
var testHtml = "aa<H1>AAA<h1/><b>asdasd<b/>";
|
||||
var t = regex.IsMatch(testHtml);
|
||||
//SendMail.Main();
|
||||
|
||||
//DbDemoDataService.AddDemoData();
|
||||
//.GetAwaiter().GetResult();
|
||||
|
@ -14,6 +14,7 @@ namespace ConsoleApp1
|
||||
MailAddress to = new MailAddress("ng.frolov@autodrilling.ru");
|
||||
MailAddress from = new MailAddress("bot@autodrilling.ru");
|
||||
MailMessage message = new MailMessage(from, to);
|
||||
|
||||
message.Subject = "Using the new SMTP client.";
|
||||
message.Body = "<html><boby><H1>this is a test text</H1></boby></html>";
|
||||
message.IsBodyHtml = true;
|
||||
|
Loading…
Reference in New Issue
Block a user