MailQueue
This commit is contained in:
10
StandManager.Service/Interfaces/IMailProcessor.cs
Normal file
10
StandManager.Service/Interfaces/IMailProcessor.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using StandManager.Domain.DTO;
|
||||
using StandManager.Domain.Entita;
|
||||
|
||||
namespace StandManager.Service.Interfaces;
|
||||
|
||||
public interface IMailProcessor
|
||||
{
|
||||
MailFrom MailFrom { get; }
|
||||
Task ProcessAsync(List<MailQueue> mailQueueList, EmailConfig config, HttpClient httpClient);
|
||||
}
|
||||
10
StandManager.Service/Mail/Email.cs
Normal file
10
StandManager.Service/Mail/Email.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace StandManager.Service.Mail;
|
||||
|
||||
public class Email
|
||||
{
|
||||
public string From { get; set; }
|
||||
public List<string> To { get; set; }
|
||||
public List<string>? Cc { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public string Body { get; set; }
|
||||
}
|
||||
72
StandManager.Service/RegistrazioneMailProcessor.cs
Normal file
72
StandManager.Service/RegistrazioneMailProcessor.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using StandManager.Domain.DTO;
|
||||
using StandManager.Domain.Entita;
|
||||
using StandManager.Service.Interfaces;
|
||||
using StandManager.Service.Mail;
|
||||
|
||||
namespace StandManager.Service;
|
||||
|
||||
public class RegistrazioneMailProcessor : IMailProcessor
|
||||
{
|
||||
private readonly IManagerService _managerService;
|
||||
|
||||
public RegistrazioneMailProcessor(IManagerService managerService)
|
||||
{
|
||||
_managerService = managerService;
|
||||
}
|
||||
|
||||
public MailFrom MailFrom => MailFrom.Registrazione;
|
||||
|
||||
public async Task ProcessAsync(List<MailQueue> mailQueueList, EmailConfig _config, HttpClient httpClient)
|
||||
{
|
||||
foreach (var mailQueue in mailQueueList)
|
||||
{
|
||||
var email = new Email()
|
||||
{
|
||||
From = _config.From,
|
||||
Body = string.IsNullOrEmpty(mailQueue.Args) ? mailQueue.Body : string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? [""]),
|
||||
Cc = null,
|
||||
Subject = mailQueue.Subject,
|
||||
To = new(){"g.vitari@oaservice.it"} // mailQueue.ToList.Split(";").ToList()
|
||||
};
|
||||
var messageJson = JsonSerializer.Serialize(email);
|
||||
|
||||
// multipart
|
||||
using var form = new MultipartFormDataContent();
|
||||
form.Add(
|
||||
new StringContent(messageJson, Encoding.UTF8, "application/json"),
|
||||
"messageJson"
|
||||
);
|
||||
|
||||
//Attachments
|
||||
/*var filePath = @"C:\temp\test.pdf";
|
||||
var fileBytes = await File.ReadAllBytesAsync(filePath);
|
||||
|
||||
var fileContent = new ByteArrayContent(fileBytes);
|
||||
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
|
||||
|
||||
form.Add(
|
||||
fileContent,
|
||||
"attachments",
|
||||
Path.GetFileName(filePath)
|
||||
);*/
|
||||
try
|
||||
{
|
||||
var response = await httpClient.PostAsync(
|
||||
_config.ServerAddress,
|
||||
form
|
||||
);
|
||||
response.EnsureSuccessStatusCode();
|
||||
mailQueue.Sent = true;
|
||||
await _managerService.MailQueueService.Salva(mailQueue);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
mailQueue.Sent = false;
|
||||
mailQueue.Error = e.Message;
|
||||
await _managerService.MailQueueService.Salva(mailQueue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
StandManager.Service/Resolver/IMailProcessorResolver.cs
Normal file
9
StandManager.Service/Resolver/IMailProcessorResolver.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using StandManager.Domain.Entita;
|
||||
using StandManager.Service.Interfaces;
|
||||
|
||||
namespace StandManager.Service.Resolver;
|
||||
|
||||
public interface IMailProcessorResolver
|
||||
{
|
||||
IMailProcessor Resolve(MailFrom mailFrom);
|
||||
}
|
||||
22
StandManager.Service/Resolver/MailProcessorResolver.cs
Normal file
22
StandManager.Service/Resolver/MailProcessorResolver.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using StandManager.Domain.Entita;
|
||||
using StandManager.Service.Interfaces;
|
||||
|
||||
namespace StandManager.Service.Resolver;
|
||||
|
||||
public class MailProcessorResolver : IMailProcessorResolver
|
||||
{
|
||||
private readonly Dictionary<MailFrom, IMailProcessor> _processors;
|
||||
|
||||
public MailProcessorResolver(IEnumerable<IMailProcessor> processors)
|
||||
{
|
||||
_processors = processors.ToDictionary(p => p.MailFrom);
|
||||
}
|
||||
|
||||
public IMailProcessor Resolve(MailFrom mailFrom)
|
||||
{
|
||||
if (!_processors.TryGetValue(mailFrom, out var processor))
|
||||
throw new NotSupportedException($"MailFrom {mailFrom} non supportato");
|
||||
|
||||
return processor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user