77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
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)
|
|
{
|
|
// Default size dei chunck è 20
|
|
var size = _config.RangeSize != 0 ? _config.RangeSize : 20;
|
|
// Estraggo i destinatari
|
|
var to = _config.To.Split(_config.MailSplitCharTo).ToList();
|
|
|
|
foreach (var chunk in mailQueueList.Chunk(size))
|
|
{
|
|
|
|
string body = string.Join(_config.MailSplitCharBody, chunk.Select(x => x.ToList));
|
|
|
|
try
|
|
{
|
|
var email = new Email()
|
|
{
|
|
From = _config.From,
|
|
Body = body,
|
|
Cc = null,
|
|
Subject = _config.Subject,
|
|
To = //to
|
|
new() { "g.vitari@oaservice.it" }
|
|
};
|
|
|
|
var messageJson = JsonSerializer.Serialize(email);
|
|
|
|
// multipart
|
|
using var form = new MultipartFormDataContent();
|
|
form.Add(new StringContent(messageJson, Encoding.UTF8, "application/json"), "messageJson");
|
|
|
|
//Attachments
|
|
/* if (File.Exists(@"C:\temp\test.pdf")) {
|
|
var fileBytes = await File.ReadAllBytesAsync(@"C:\temp\test.pdf");
|
|
var fileContent = new ByteArrayContent(fileBytes);
|
|
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/pdf");
|
|
form.Add(fileContent, "attachments", "test.pdf");
|
|
}
|
|
*/
|
|
|
|
var response = await httpClient.PostAsync(
|
|
_config.ServerAddress,
|
|
form
|
|
);
|
|
response.EnsureSuccessStatusCode();
|
|
Array.ForEach(chunk, item => item.Sent = true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Array.ForEach(chunk, item => item.Sent = false);
|
|
Array.ForEach(chunk, item => item.Error = e.Message);
|
|
}
|
|
|
|
// Salvo in blocco le entità
|
|
await _managerService.MailQueueService.Salva(chunk.ToList());
|
|
}
|
|
}
|
|
} |