MailQueue
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user