- FIX scheduler
- Ottimizzazione funzione invio mail scheduler
This commit is contained in:
@ -24,55 +24,58 @@ public class RegistrazioneMailProcessor : IMailProcessor
|
||||
|
||||
foreach (var chunk in mailQueueList.Chunk(size))
|
||||
{
|
||||
foreach (var mailQueue in chunk)
|
||||
var emailTasks = chunk.Select(async mailQueue =>
|
||||
{
|
||||
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 email = new Email()
|
||||
{
|
||||
From = _config.From,
|
||||
Body = string.IsNullOrEmpty(mailQueue.Args)
|
||||
? mailQueue.Body
|
||||
: string.Format(mailQueue.Body, mailQueue.Args?.Split(_config.MailSplitChar) ?? Array.Empty<string>()),
|
||||
Cc = null,
|
||||
Subject = mailQueue.Subject,
|
||||
To = mailQueue.ToList.Split(";").ToList() // 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();
|
||||
mailQueue.Sent = true;
|
||||
await _managerService.MailQueueService.Salva(mailQueue);
|
||||
mailQueue.Error = null;
|
||||
return mailQueue;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
mailQueue.Sent = false;
|
||||
mailQueue.Error = e.Message;
|
||||
await _managerService.MailQueueService.Salva(mailQueue);
|
||||
return mailQueue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Prendo i risultati dell'invio
|
||||
var results = await Task.WhenAll(emailTasks);
|
||||
|
||||
// Salvo in blocco le entità
|
||||
await _managerService.MailQueueService.Salva(results.ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user