MailProcessor
This commit is contained in:
17
StandManager.MailProcessor/Mail/Email.cs
Normal file
17
StandManager.MailProcessor/Mail/Email.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace StandManager.MailProcessor.Mail;
|
||||
|
||||
public class Email
|
||||
{
|
||||
public string From { get; set; }
|
||||
public List<string> To { get; set; }
|
||||
public List<string>? Cc { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public string Body { get; set; }
|
||||
}
|
||||
|
||||
public class EmailConfig
|
||||
{
|
||||
public string From { get; set; }
|
||||
public string ServerAddress { get; set; }
|
||||
public string MailSplitChar { get; set; }
|
||||
}
|
||||
@ -1,18 +1,77 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using StandManager.MailProcessor.Mail;
|
||||
using StandManager.Service.Interfaces;
|
||||
|
||||
public class MailProcessor
|
||||
{
|
||||
private readonly IManagerService _managerService;
|
||||
private readonly EmailConfig _config;
|
||||
|
||||
public MailProcessor(IManagerService managerService)
|
||||
public MailProcessor(IManagerService managerService, EmailConfig config)
|
||||
{
|
||||
_managerService = managerService;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public async Task Process()
|
||||
{
|
||||
var list = await _managerService.MailQueueService.RicercaQueryable(x =>
|
||||
x.Eliminato == false && x.Sent == false);
|
||||
x.Eliminato == false && x.Sent == false && string.IsNullOrEmpty(x.Error), solaLettura:false);
|
||||
|
||||
using var httpClient = new HttpClient();
|
||||
foreach (var mailQueue in list)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var a = list;
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using StandManager.Infrastructure.DAL.Context;
|
||||
using StandManager.MailProcessor.Mail;
|
||||
using StandManager.Service;
|
||||
using StandManager.Service.Interfaces;
|
||||
using StandManager.Service.Repository;
|
||||
@ -19,7 +20,11 @@ var host = Host.CreateDefaultBuilder(args)
|
||||
var impl = allProviderTypes.FirstOrDefault(c => c.IsClass && intfc.Name[1..] == c.Name);
|
||||
if (impl != null) services.AddScoped(intfc, impl);
|
||||
}
|
||||
//Database
|
||||
|
||||
var emailConfigSection = context.Configuration.GetSection("EmailConfig");
|
||||
services.Configure<EmailConfig>(emailConfigSection);
|
||||
|
||||
//Database
|
||||
var connectionString = context.Configuration.GetConnectionString("ConnectionString");
|
||||
services.AddDbContext<StandManagerDbContext>(options =>
|
||||
options.UseSqlServer(connectionString)
|
||||
|
||||
@ -8,5 +8,10 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"EmailConfig": {
|
||||
"From": "",
|
||||
"ServerAddress": "",
|
||||
"MailSplitChar": "#"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user