MailQueue

This commit is contained in:
2026-01-20 16:47:24 +01:00
parent a5fe908034
commit d40e2e8daa
13 changed files with 181 additions and 71 deletions

View File

@ -1,20 +1,18 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using StandManager.Domain.DTO;
using StandManager.MailProcessor.Mail;
using StandManager.Service.Interfaces;
using StandManager.Service.Resolver;
public class MailProcessor
{
private readonly IManagerService _managerService;
private readonly EmailConfig _config;
private readonly IMailProcessorResolver _resolver;
public MailProcessor(IManagerService managerService, EmailConfig config)
public MailProcessor(IManagerService managerService, EmailConfig config, IMailProcessorResolver resolver)
{
_managerService = managerService;
_config = config;
_resolver = resolver;
}
public async Task Process()
@ -23,54 +21,11 @@ public class MailProcessor
x.Eliminato == false && x.Sent == false && string.IsNullOrEmpty(x.Error), solaLettura:false);
using var httpClient = new HttpClient();
foreach (var mailQueue in list)
foreach (var mailQueueByFrom in list.GroupBy(x => x.From))
{
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 processor = _resolver.Resolve(mailQueueByFrom.Key);
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);
}
await processor.ProcessAsync(mailQueueByFrom.ToList(), _config, httpClient);
}
var a = list;