If you like this repository, please drop a โญ on Github!
The library is available on NuGet or run the following command in the .NET CLI:
dotnet add package MailKitCustomExtensions
"Serilog": {
"MinimumLevel": "Warning",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:HH:mm:ss}\t{Level:u3}\t{SourceContext}\t{Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "Logs/log.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 14,
"restrictedToMinimumLevel": "Warning",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]
},
"Smtp": {
"Host": "example.org",
"Port": 25,
"Security": "StartTls",
"Username": "Username SMTP",
"Password": "Password SMTP",
"Sender": "MyName <[email protected]>"
}
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//Includes registration of services.AddSerilogServices() from Nuget package SerilogCustomExtensions
services.AddMailKitEmailSenderService(Configuration);
}
//OMISSIS
public void Configure(WebApplication app)
{
//Dependency Injection needed by the SerilogCustomExtensions Nuget package
app.AddSerilogConfigureServices();
}
public static void Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
//Dependency Injection needed by the SerilogCustomExtensions Nuget package
builder.AddSerilogOptionsBuilder();
Startup startup = new(builder.Configuration);
//OMISSIS
}
public class InputMailSender
{
public string RecipientEmail { get; set; }
public string ReplyToEmail { get; set; }
public string Subject { get; set; }
public string HtmlMessage { get; set; }
}
[ApiController]
[Route("api/[controller]")]
public class EmailController : ControllerBase
{
private readonly ILoggerService loggerService;
private readonly IEmailClient emailClient;
public EmailController(ILoggerService loggerService, IEmailClient emailClient)
{
this.loggerService = loggerService;
this.emailClient = emailClient;
}
[HttpPost("InvioEmail")]
public async Task<IActionResult> InvioEmail([FromForm] InputMailSender model)
{
try
{
var result = await emailClient.SendEmailAsync(model.RecipientEmail, model.ReplyToEmail, model.Subject, model.HtmlMessage);
if (!result)
{
return BadRequest();
}
return Ok();
}
catch
{
var message = "Errore durante l'invio della mail";
var errori = loggerService.ManageError(message, 400, 0, HttpContext);
return StatusCode(400, errori);
}
}
}
If you are using this Nuget package, you can avoid using the SerilogCustomExtensions package, as it is already integrated in this one.
Contributions and/or suggestions are always welcome.