- FIX Salvataggio mail Registrazione
- Aggiunta RangeSize in scheduler
This commit is contained in:
@ -5,4 +5,5 @@ public class EmailConfig
|
||||
public string From { get; set; }
|
||||
public string ServerAddress { get; set; }
|
||||
public string MailSplitChar { get; set; }
|
||||
public int RangeSize { get; set; }
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using StandManager.Domain.DTO;
|
||||
using StandManager.Service.Interfaces;
|
||||
using StandManager.Service.Resolver;
|
||||
@ -8,10 +9,10 @@ public class MailProcessor
|
||||
private readonly EmailConfig _config;
|
||||
private readonly IMailProcessorResolver _resolver;
|
||||
|
||||
public MailProcessor(IManagerService managerService, EmailConfig config, IMailProcessorResolver resolver)
|
||||
public MailProcessor(IManagerService managerService, IOptions<EmailConfig> config, IMailProcessorResolver resolver)
|
||||
{
|
||||
_managerService = managerService;
|
||||
_config = config;
|
||||
_config = config.Value;
|
||||
_resolver = resolver;
|
||||
}
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
"EmailConfig": {
|
||||
"From": "registrazioni@gruppodac.eu",
|
||||
"ServerAddress": "https://mailbridge.gruppodac.eu/api/graph/NewMessageHtml",
|
||||
"MailSplitChar": "#"
|
||||
"MailSplitChar": "#",
|
||||
"RangeSize": 20
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,9 @@ public class MailQueueService : TService<MailQueue>, IMailQueueService
|
||||
|
||||
public async Task<MailQueue> Salva(MailQueue entity)
|
||||
{
|
||||
_unitOfWork.MailQueueRepository.Update(entity);
|
||||
if (entity.Id == Guid.Empty) await _unitOfWork.MailQueueRepository.Put(entity);
|
||||
else _unitOfWork.MailQueueRepository.Update(entity);
|
||||
|
||||
await _unitOfWork.Salva();
|
||||
|
||||
return entity;
|
||||
|
||||
@ -20,52 +20,58 @@ public class RegistrazioneMailProcessor : IMailProcessor
|
||||
|
||||
public async Task ProcessAsync(List<MailQueue> mailQueueList, EmailConfig _config, HttpClient httpClient)
|
||||
{
|
||||
foreach (var mailQueue in mailQueueList)
|
||||
var size = _config.RangeSize != 0 ? _config.RangeSize : mailQueueList.Count;
|
||||
|
||||
foreach (var chunk in mailQueueList.Chunk(size))
|
||||
{
|
||||
var email = new Email()
|
||||
foreach (var mailQueue in chunk)
|
||||
{
|
||||
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);
|
||||
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
|
||||
// multipart
|
||||
using var form = new MultipartFormDataContent();
|
||||
form.Add(
|
||||
new StringContent(messageJson, Encoding.UTF8, "application/json"),
|
||||
"messageJson"
|
||||
);
|
||||
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);
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,15 +90,16 @@
|
||||
};
|
||||
await _managerService.IscrizioneEventoPerMailService.Salva(model);
|
||||
|
||||
/*var queue = new MailQueue()
|
||||
var queue = new MailQueue()
|
||||
{
|
||||
ToList = iscrizione.Mail,
|
||||
From = MailFrom.Registrazione,
|
||||
Body = string.Empty,
|
||||
Sent = false,
|
||||
Subject = "Registrazione"
|
||||
Subject = "Registrazione",
|
||||
DataCreazione = DateTime.Now
|
||||
};
|
||||
await _managerService.MailQueueService.Salva(queue);*/
|
||||
await _managerService.MailQueueService.Salva(queue);
|
||||
|
||||
_navManager.NavigateTo("/Grazie");
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
"EmailConfig": {
|
||||
"From": "",
|
||||
"ServerAddress": "",
|
||||
"MailSplitChar": "#"
|
||||
"MailSplitChar": "#",
|
||||
"RangeSize": 0
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user