mailer handler

master
Tudor Stanciu 2023-01-19 18:17:24 +02:00
parent 3b650cb099
commit c3b0fb60df
21 changed files with 316 additions and 17 deletions

View File

@ -24,6 +24,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.PublishedLanguage",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Domain", "src\Correo.Domain\Correo.Domain.csproj", "{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.SmtpClient", "src\Correo.SmtpClient\Correo.SmtpClient.csproj", "{76793477-8219-4FFB-AA08-3F2224EC4968}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Abstractions", "src\Correo.Abstractions\Correo.Abstractions.csproj", "{ED8048DC-8509-4085-97F0-F9D9A59A689A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -46,6 +50,14 @@ Global
{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Release|Any CPU.Build.0 = Release|Any CPU
{76793477-8219-4FFB-AA08-3F2224EC4968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76793477-8219-4FFB-AA08-3F2224EC4968}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76793477-8219-4FFB-AA08-3F2224EC4968}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76793477-8219-4FFB-AA08-3F2224EC4968}.Release|Any CPU.Build.0 = Release|Any CPU
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -55,6 +67,8 @@ Global
{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{DA47BCEE-9AE7-4F20-A04F-5866966503C5} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{A2D2694C-AB68-49B0-B4B0-94BBFF48C043} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{76793477-8219-4FFB-AA08-3F2224EC4968} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{ED8048DC-8509-4085-97F0-F9D9A59A689A} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D}

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Correo.Domain.Models
namespace Correo.Abstractions
{
public record EmailMessage
{

View File

@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;
namespace Correo.Abstractions
{
public interface IMailer
{
void SendEmail(EmailMessage message);
Task SendEmailAsync(EmailMessage message, CancellationToken token = default);
}
}

View File

@ -1,4 +1,5 @@
using AutoMapper;
using Correo.Abstractions;
using Correo.Application.Extensions;
using Correo.Domain.Models;
using Correo.PublishedLanguage.Commands;
@ -16,16 +17,18 @@ namespace Correo.Application.CommandHandlers
public class SendEmailHandler : IRequestHandler<SendEmail>
{
private readonly IMessageBusPublisher _messageBusPublisher;
private readonly IOptions<DefaultSender> _defaultSender;
private readonly ILogger<SendEmailHandler> _logger;
private readonly IMapper _mapper;
private readonly IOptions<DefaultSender> _defaultSender;
private readonly IMailer _mailer;
public SendEmailHandler(IMessageBusPublisher messageBusPublisher, ILogger<SendEmailHandler> logger, IMapper mapper, IOptions<DefaultSender> defaultSender)
public SendEmailHandler(IMessageBusPublisher messageBusPublisher, IOptions<DefaultSender> defaultSender, ILogger<SendEmailHandler> logger, IMapper mapper, IMailer mailer)
{
_messageBusPublisher=messageBusPublisher;
_defaultSender=defaultSender;
_logger=logger;
_mapper=mapper;
_defaultSender=defaultSender;
_mailer=mailer;
}
public async Task<Unit> Handle(SendEmail command, CancellationToken cancellationToken)
@ -33,15 +36,14 @@ namespace Correo.Application.CommandHandlers
try
{
var emailMessage = _mapper.Map<EmailMessage>(command);
if (emailMessage.From == null)
emailMessage.From = new EmailMessage.MailAddress(_defaultSender.Value.Address, _defaultSender.Value.Name);
emailMessage.Enrich(_defaultSender.Value);
emailMessage.Validate();
// send email
await _mailer.SendEmailAsync(emailMessage, cancellationToken);
await _messageBusPublisher.PublishAsync(new EmailSent(command.Subject), cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
await _messageBusPublisher.PublishAsync(new EmailSentFailed(command.Subject, ex.Message), cancellationToken);
throw;
}

View File

@ -0,0 +1,37 @@
using Correo.Abstractions;
using Correo.Application.Extensions;
using Correo.Domain.Models;
using MediatR;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
namespace Correo.Application.CommandHandlers
{
public class SendEmailHandlerSync
{
public record Command : EmailMessage, IRequest<Unit>
{
}
public class CommandHandler : IRequestHandler<Command, Unit>
{
private readonly IOptions<DefaultSender> _defaultSender;
private readonly IMailer _mailer;
public CommandHandler(IOptions<DefaultSender> defaultSender, IMailer mailer)
{
_defaultSender=defaultSender;
_mailer=mailer;
}
public async Task<Unit> Handle(Command command, CancellationToken cancellationToken)
{
command.Enrich(_defaultSender.Value);
command.Validate();
await _mailer.SendEmailAsync(command, cancellationToken);
return Unit.Value;
}
}
}
}

View File

@ -11,8 +11,10 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Correo.Abstractions\Correo.Abstractions.csproj" />
<ProjectReference Include="..\Correo.Domain\Correo.Domain.csproj" />
<ProjectReference Include="..\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj" />
<ProjectReference Include="..\Correo.SmtpClient\Correo.SmtpClient.csproj" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using Correo.Domain.Models;
using Correo.SmtpClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -9,6 +10,7 @@ namespace Correo.Application
public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<DefaultSender>(configuration.GetSection("DefaultSender"));
services.AddSmtpClientServices(configuration.GetSection("SmtpClient"));
}
}
}

View File

@ -0,0 +1,14 @@
using Correo.Abstractions;
using Correo.Domain.Models;
namespace Correo.Application.Extensions
{
internal static class ModelExtensions
{
public static void Enrich(this EmailMessage emailMessage, DefaultSender defaultSender)
{
if (emailMessage.From == null)
emailMessage.From = new EmailMessage.MailAddress(defaultSender.Address, defaultSender.Name);
}
}
}

View File

@ -1,5 +1,5 @@
using Correo.Application.Utils;
using Correo.Domain.Models;
using Correo.Abstractions;
using Correo.Application.Utils;
using System;
using System.Linq;

View File

@ -1,5 +1,6 @@
using AutoMapper;
using Correo.Domain.Models;
using Correo.Abstractions;
using Correo.Application.CommandHandlers;
using Correo.PublishedLanguage.Commands;
namespace Correo.Application.Mappings
@ -10,6 +11,7 @@ namespace Correo.Application.Mappings
{
CreateMap<SendEmail.MailAddress, EmailMessage.MailAddress>();
CreateMap<SendEmail, EmailMessage>();
CreateMap<SendEmail, SendEmailHandlerSync.Command>();
}
}
}

View File

@ -1,5 +1,7 @@
using MediatR;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@ -25,10 +27,22 @@ namespace Correo.Application.Queries
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
{
var version = Environment.GetEnvironmentVariable("APP_VERSION");
var appDate = Environment.GetEnvironmentVariable("APP_DATE");
if (string.IsNullOrEmpty(version))
version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
if (!DateTime.TryParse(appDate, out var lastUpdateDate))
{
var location = Assembly.GetExecutingAssembly().Location;
lastUpdateDate = File.GetLastWriteTime(location);
}
var result = new Model()
{
Version = "1.0.0",
LastUpdateDate = DateTime.Now
Version = version,
LastUpdateDate = lastUpdateDate
};
return await Task.FromResult(result);

View File

@ -1,4 +1,8 @@
namespace Correo.Domain.Models
{
public record DefaultSender(string Address, string Name);
public record DefaultSender
{
public string Address { get; init; }
public string Name { get; init; }
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Correo.Abstractions\Correo.Abstractions.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
using Correo.Abstractions;
using Correo.SmtpClient.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Correo.SmtpClient
{
public static class DependencyInjectionExtensions
{
public static void AddSmtpClientServices(this IServiceCollection services, IConfigurationSection configuration)
{
services.Configure<SmtpClientOptions>(configuration);
services.AddTransient<IMailer, SmtpClientMailer>();
}
}
}

View File

@ -0,0 +1,39 @@
using Correo.Abstractions;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
namespace Correo.SmtpClient.Extensions
{
internal static class ModelExtensions
{
public static string Log(this IEnumerable<EmailMessage.MailAddress> addresses)
=> addresses != null ? string.Join(',', addresses.Select(z => z.Address)) : string.Empty;
public static MailAddress ToMailAddress(this EmailMessage.MailAddress address)
=> new MailAddress(address.Address, address.DisplayName);
public static void AddRange(this MailAddressCollection collection, IEnumerable<EmailMessage.MailAddress> addresses)
{
foreach (var item in addresses)
collection.Add(item.ToMailAddress());
}
public static MailMessage ToMailMessage(this EmailMessage message)
{
var mailMessage = new MailMessage
{
Subject = message.Subject,
Body = message.Body,
From = message.From.ToMailAddress(),
IsBodyHtml = message.IsBodyHtml
};
mailMessage.To.AddRange(message.To);
mailMessage.CC.AddRange(message.Cc);
mailMessage.Bcc.AddRange(message.Bcc);
return mailMessage;
}
}
}

View File

@ -0,0 +1,19 @@
namespace Correo.SmtpClient.Models
{
public record SmtpClientOptions
{
public string Server { get; init; }
public int Port { get; init; }
public bool UseSsl { get; init; }
public bool UseAuthentication { get; init; }
public bool TrustServer { get; init; }
public AuthenticationOptions Authentication { get; init; }
public record AuthenticationOptions
{
public string UserName { get; init; }
public string Domain { get; init; }
public string Password { get; init; }
}
}
}

View File

@ -0,0 +1,64 @@
using Correo.Abstractions;
using Correo.SmtpClient.Extensions;
using Correo.SmtpClient.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace Correo.SmtpClient
{
public class SmtpClientMailer : IMailer, IDisposable
{
private readonly IOptions<SmtpClientOptions> _optionsAccessor;
private readonly System.Net.Mail.SmtpClient _smtpClient;
private readonly ILogger<SmtpClientMailer> _logger;
public SmtpClientMailer(IOptions<SmtpClientOptions> optionsAccessor, ILogger<SmtpClientMailer> logger)
{
_optionsAccessor = optionsAccessor;
_logger = logger;
var options = _optionsAccessor.Value;
_smtpClient = new System.Net.Mail.SmtpClient { Host = options.Server, Port = options.Port, EnableSsl = options.UseSsl };
if (options.UseAuthentication)
{
if (string.IsNullOrEmpty(options.Authentication.Domain))
_smtpClient.Credentials = new NetworkCredential(options.Authentication.UserName, options.Authentication.Password);
else
_smtpClient.Credentials = new NetworkCredential(options.Authentication.UserName, options.Authentication.Password, options.Authentication.Domain);
}
else
_smtpClient.UseDefaultCredentials = true;
if (options.TrustServer)
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
}
public void Dispose()
{
_smtpClient?.Dispose();
GC.SuppressFinalize(this);
}
public void SendEmail(EmailMessage message)
{
var mailMessage = message.ToMailMessage();
_smtpClient.Send(mailMessage);
Log(message);
}
public async Task SendEmailAsync(EmailMessage message, CancellationToken token = default)
{
var mailMessage = message.ToMailMessage();
await _smtpClient.SendMailAsync(mailMessage, token);
Log(message);
}
private void Log(EmailMessage message)
=> _logger.LogInformation($"Email sent: Subject: {message.Subject}; From: {message.From.Address}; To: {message.To.Log()}; Cc: {message.Cc.Log()}; Bcc: {message.Bcc.Log()};");
}
}

View File

@ -0,0 +1,31 @@
using AutoMapper;
using Correo.Application.CommandHandlers;
using Correo.PublishedLanguage.Commands;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Correo.Controllers
{
[ApiController]
[Route("api/mailer")]
public class MailerController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IMapper _mapper;
public MailerController(IMediator mediator, IMapper mapper)
{
_mediator=mediator;
_mapper=mapper;
}
[HttpPost("email")]
public async Task<IActionResult> SendEmail([FromBody] SendEmail sendEmail)
{
var command = _mapper.Map<SendEmailHandlerSync.Command>(sendEmail);
var result = await _mediator.Send(command);
return Ok(result);
}
}
}

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Correo.Controllers
{
[ApiController]
[Route("[controller]")]
[Route("api/system")]
public class SystemController : ControllerBase
{
private readonly IMediator _mediator;

View File

@ -1,4 +1,5 @@
using MediatR;
using Correo.Application;
using MediatR;
using MediatR.Pipeline;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
@ -27,6 +28,9 @@ namespace Correo.Extensions
// Messaging
services.AddMessaging(configuration);
// Application services
services.AddApplicationServices(configuration);
}
public static void Configure(this WebApplication app)