mailer handler
parent
3b650cb099
commit
c3b0fb60df
14
Correo.sln
14
Correo.sln
|
@ -24,6 +24,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.PublishedLanguage",
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Domain", "src\Correo.Domain\Correo.Domain.csproj", "{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Domain", "src\Correo.Domain\Correo.Domain.csproj", "{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}"
|
||||||
EndProject
|
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
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Release|Any CPU.Build.0 = 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
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -55,6 +67,8 @@ Global
|
||||||
{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
|
{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
|
||||||
{DA47BCEE-9AE7-4F20-A04F-5866966503C5} = {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}
|
{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
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D}
|
SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -1,6 +1,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Correo.Domain.Models
|
namespace Correo.Abstractions
|
||||||
{
|
{
|
||||||
public record EmailMessage
|
public record EmailMessage
|
||||||
{
|
{
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using Correo.Abstractions;
|
||||||
using Correo.Application.Extensions;
|
using Correo.Application.Extensions;
|
||||||
using Correo.Domain.Models;
|
using Correo.Domain.Models;
|
||||||
using Correo.PublishedLanguage.Commands;
|
using Correo.PublishedLanguage.Commands;
|
||||||
|
@ -16,16 +17,18 @@ namespace Correo.Application.CommandHandlers
|
||||||
public class SendEmailHandler : IRequestHandler<SendEmail>
|
public class SendEmailHandler : IRequestHandler<SendEmail>
|
||||||
{
|
{
|
||||||
private readonly IMessageBusPublisher _messageBusPublisher;
|
private readonly IMessageBusPublisher _messageBusPublisher;
|
||||||
|
private readonly IOptions<DefaultSender> _defaultSender;
|
||||||
private readonly ILogger<SendEmailHandler> _logger;
|
private readonly ILogger<SendEmailHandler> _logger;
|
||||||
private readonly IMapper _mapper;
|
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;
|
_messageBusPublisher=messageBusPublisher;
|
||||||
|
_defaultSender=defaultSender;
|
||||||
_logger=logger;
|
_logger=logger;
|
||||||
_mapper=mapper;
|
_mapper=mapper;
|
||||||
_defaultSender=defaultSender;
|
_mailer=mailer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Unit> Handle(SendEmail command, CancellationToken cancellationToken)
|
public async Task<Unit> Handle(SendEmail command, CancellationToken cancellationToken)
|
||||||
|
@ -33,15 +36,14 @@ namespace Correo.Application.CommandHandlers
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var emailMessage = _mapper.Map<EmailMessage>(command);
|
var emailMessage = _mapper.Map<EmailMessage>(command);
|
||||||
if (emailMessage.From == null)
|
emailMessage.Enrich(_defaultSender.Value);
|
||||||
emailMessage.From = new EmailMessage.MailAddress(_defaultSender.Value.Address, _defaultSender.Value.Name);
|
|
||||||
|
|
||||||
emailMessage.Validate();
|
emailMessage.Validate();
|
||||||
|
await _mailer.SendEmailAsync(emailMessage, cancellationToken);
|
||||||
// send email
|
await _messageBusPublisher.PublishAsync(new EmailSent(command.Subject), cancellationToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
_logger.LogError(ex, ex.Message);
|
||||||
await _messageBusPublisher.PublishAsync(new EmailSentFailed(command.Subject, ex.Message), cancellationToken);
|
await _messageBusPublisher.PublishAsync(new EmailSentFailed(command.Subject, ex.Message), cancellationToken);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,8 +11,10 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Correo.Abstractions\Correo.Abstractions.csproj" />
|
||||||
<ProjectReference Include="..\Correo.Domain\Correo.Domain.csproj" />
|
<ProjectReference Include="..\Correo.Domain\Correo.Domain.csproj" />
|
||||||
<ProjectReference Include="..\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj" />
|
<ProjectReference Include="..\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj" />
|
||||||
|
<ProjectReference Include="..\Correo.SmtpClient\Correo.SmtpClient.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using Correo.Domain.Models;
|
using Correo.Domain.Models;
|
||||||
|
using Correo.SmtpClient;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ namespace Correo.Application
|
||||||
public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
|
public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.Configure<DefaultSender>(configuration.GetSection("DefaultSender"));
|
services.Configure<DefaultSender>(configuration.GetSection("DefaultSender"));
|
||||||
|
services.AddSmtpClientServices(configuration.GetSection("SmtpClient"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
using Correo.Application.Utils;
|
using Correo.Abstractions;
|
||||||
using Correo.Domain.Models;
|
using Correo.Application.Utils;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Correo.Domain.Models;
|
using Correo.Abstractions;
|
||||||
|
using Correo.Application.CommandHandlers;
|
||||||
using Correo.PublishedLanguage.Commands;
|
using Correo.PublishedLanguage.Commands;
|
||||||
|
|
||||||
namespace Correo.Application.Mappings
|
namespace Correo.Application.Mappings
|
||||||
|
@ -10,6 +11,7 @@ namespace Correo.Application.Mappings
|
||||||
{
|
{
|
||||||
CreateMap<SendEmail.MailAddress, EmailMessage.MailAddress>();
|
CreateMap<SendEmail.MailAddress, EmailMessage.MailAddress>();
|
||||||
CreateMap<SendEmail, EmailMessage>();
|
CreateMap<SendEmail, EmailMessage>();
|
||||||
|
CreateMap<SendEmail, SendEmailHandlerSync.Command>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -25,10 +27,22 @@ namespace Correo.Application.Queries
|
||||||
|
|
||||||
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
|
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()
|
var result = new Model()
|
||||||
{
|
{
|
||||||
Version = "1.0.0",
|
Version = version,
|
||||||
LastUpdateDate = DateTime.Now
|
LastUpdateDate = lastUpdateDate
|
||||||
};
|
};
|
||||||
|
|
||||||
return await Task.FromResult(result);
|
return await Task.FromResult(result);
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
namespace Correo.Domain.Models
|
namespace Correo.Domain.Models
|
||||||
{
|
{
|
||||||
public record DefaultSender(string Address, string Name);
|
public record DefaultSender
|
||||||
|
{
|
||||||
|
public string Address { get; init; }
|
||||||
|
public string Name { get; init; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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>
|
|
@ -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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()};");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||||
namespace Correo.Controllers
|
namespace Correo.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("api/system")]
|
||||||
public class SystemController : ControllerBase
|
public class SystemController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using MediatR;
|
using Correo.Application;
|
||||||
|
using MediatR;
|
||||||
using MediatR.Pipeline;
|
using MediatR.Pipeline;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
@ -27,6 +28,9 @@ namespace Correo.Extensions
|
||||||
|
|
||||||
// Messaging
|
// Messaging
|
||||||
services.AddMessaging(configuration);
|
services.AddMessaging(configuration);
|
||||||
|
|
||||||
|
// Application services
|
||||||
|
services.AddApplicationServices(configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Configure(this WebApplication app)
|
public static void Configure(this WebApplication app)
|
||||||
|
|
Loading…
Reference in New Issue