- Scheduler aggiornato
This commit is contained in:
@ -4,6 +4,9 @@ public class EmailConfig
|
||||
{
|
||||
public string From { get; set; }
|
||||
public string ServerAddress { get; set; }
|
||||
public string MailSplitChar { get; set; }
|
||||
public string MailSplitCharTo { get; set; }
|
||||
public int RangeSize { get; set; }
|
||||
public string To { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public string MailSplitCharBody { get; set; }
|
||||
}
|
||||
@ -10,9 +10,12 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"EmailConfig": {
|
||||
"From": "registrazioni@gruppodac.eu",
|
||||
"ServerAddress": "https://mailbridge.gruppodac.eu/api/graph/NewMessageHtml",
|
||||
"MailSplitChar": ";",
|
||||
"RangeSize": 20
|
||||
"RangeSize": 3,
|
||||
"From": "registrazioni@gruppodac.eu",
|
||||
"MailSplitCharTo": ";",
|
||||
"To": "g.vitari@oaservice.it;d.sandrelli@oaservice.it",
|
||||
"Subject": "Registrazione",
|
||||
"MailSplitCharBody": ";"
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,68 +15,64 @@ public class RegistrazioneMailProcessor : IMailProcessor
|
||||
{
|
||||
_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))
|
||||
{
|
||||
var emailTasks = chunk.Select(async mailQueue =>
|
||||
|
||||
string body = string.Join(_config.MailSplitCharBody, chunk.Select(x => x.ToList));
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var email = new Email()
|
||||
{
|
||||
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(_config.MailSplitChar).ToList() // new() { "g.vitari@oaservice.it" }
|
||||
};
|
||||
From = _config.From,
|
||||
Body = body,
|
||||
Cc = null,
|
||||
Subject = _config.Subject,
|
||||
To = to // new() { "g.vitari@oaservice.it" }
|
||||
};
|
||||
|
||||
var messageJson = JsonSerializer.Serialize(email);
|
||||
var messageJson = JsonSerializer.Serialize(email);
|
||||
|
||||
// multipart
|
||||
using var form = new MultipartFormDataContent();
|
||||
form.Add(new StringContent(messageJson, Encoding.UTF8, "application/json"), "messageJson");
|
||||
// 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");
|
||||
}
|
||||
*/
|
||||
//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;
|
||||
mailQueue.Error = null;
|
||||
return mailQueue;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
mailQueue.Sent = false;
|
||||
mailQueue.Error = e.Message;
|
||||
return mailQueue;
|
||||
}
|
||||
});
|
||||
|
||||
// Prendo i risultati dell'invio
|
||||
var results = await Task.WhenAll(emailTasks);
|
||||
var response = await httpClient.PostAsync(
|
||||
_config.ServerAddress,
|
||||
form
|
||||
);
|
||||
response.EnsureSuccessStatusCode();
|
||||
if (chunk.First().ToList.Contains("4"))
|
||||
throw new Exception("Simulazione errore di invio");
|
||||
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(results.ToList());
|
||||
await _managerService.MailQueueService.Salva(chunk.ToList());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,9 +10,12 @@
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"EmailConfig": {
|
||||
"From": "",
|
||||
"ServerAddress": "",
|
||||
"MailSplitChar": ";",
|
||||
"RangeSize": 0
|
||||
"RangeSize": 0,
|
||||
"From": "",
|
||||
"MailSplitCharTo": ";",
|
||||
"To": "",
|
||||
"Subject": "",
|
||||
"MailSplitCharBody": ";"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user