Added MailKit

master
Tudor Stanciu 2023-01-19 19:45:00 +02:00
parent dc5ce5e96a
commit 31dfbe1ccf
17 changed files with 219 additions and 20 deletions

View File

@ -18,15 +18,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Application", "src\Correo.Application\Correo.Application.csproj", "{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Correo.Application", "src\Correo.Application\Correo.Application.csproj", "{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.PublishedLanguage", "src\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj", "{DA47BCEE-9AE7-4F20-A04F-5866966503C5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Correo.PublishedLanguage", "src\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj", "{DA47BCEE-9AE7-4F20-A04F-5866966503C5}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Domain", "src\Correo.Domain\Correo.Domain.csproj", "{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Correo.SmtpClient", "src\Correo.SmtpClient\Correo.SmtpClient.csproj", "{76793477-8219-4FFB-AA08-3F2224EC4968}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Abstractions", "src\Correo.Abstractions\Correo.Abstractions.csproj", "{ED8048DC-8509-4085-97F0-F9D9A59A689A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Correo.Abstractions", "src\Correo.Abstractions\Correo.Abstractions.csproj", "{ED8048DC-8509-4085-97F0-F9D9A59A689A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.MailKit", "src\Correo.MailKit\Correo.MailKit.csproj", "{325B77E1-D752-4578-8BF7-793905C38DCD}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -58,6 +60,10 @@ Global
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
{ED8048DC-8509-4085-97F0-F9D9A59A689A}.Release|Any CPU.Build.0 = Release|Any CPU {ED8048DC-8509-4085-97F0-F9D9A59A689A}.Release|Any CPU.Build.0 = Release|Any CPU
{325B77E1-D752-4578-8BF7-793905C38DCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{325B77E1-D752-4578-8BF7-793905C38DCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{325B77E1-D752-4578-8BF7-793905C38DCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{325B77E1-D752-4578-8BF7-793905C38DCD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -69,6 +75,7 @@ Global
{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} {76793477-8219-4FFB-AA08-3F2224EC4968} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{ED8048DC-8509-4085-97F0-F9D9A59A689A} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} {ED8048DC-8509-4085-97F0-F9D9A59A689A} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
{325B77E1-D752-4578-8BF7-793905C38DCD} = {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}

View File

@ -8,5 +8,6 @@
<AutoMapperExtensionsPackageVersion>12.0.0</AutoMapperExtensionsPackageVersion> <AutoMapperExtensionsPackageVersion>12.0.0</AutoMapperExtensionsPackageVersion>
<MediatRPackageVersion>9.0.0</MediatRPackageVersion> <MediatRPackageVersion>9.0.0</MediatRPackageVersion>
<NBBPackageVersion>6.0.30</NBBPackageVersion> <NBBPackageVersion>6.0.30</NBBPackageVersion>
<MailKitPackageVersion>3.4.3</MailKitPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Linq;
namespace Correo.Abstractions.Extensions
{
public static class ModelExtensions
{
public static string Log(this IEnumerable<EmailMessage.MailAddress> addresses)
=> addresses != null ? string.Join(',', addresses.Select(z => z.Address)) : string.Empty;
}
}

View File

@ -14,7 +14,6 @@
<ProjectReference Include="..\Correo.Abstractions\Correo.Abstractions.csproj" /> <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>

View File

@ -1,5 +1,4 @@
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;
@ -10,7 +9,6 @@ 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"));
} }
} }
} }

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="$(MailKitPackageVersion)" />
<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.MailKit.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Correo.MailKit
{
public static class DependencyInjectionExtensions
{
public static void AddMailKitSmtpClient(this IServiceCollection services, IConfigurationSection configuration)
{
services.Configure<SmtpClientOptions>(configuration);
services.AddTransient<IMailer, MailKitSmtpClient>();
}
}
}

View File

@ -0,0 +1,35 @@
using Correo.Abstractions;
using MimeKit;
using System.Collections.Generic;
namespace Correo.MailKit.Extensions
{
internal static class ModelExtensions
{
private static MailboxAddress ToMailboxAddress(this EmailMessage.MailAddress address)
=> new MailboxAddress(address.DisplayName, address.Address);
private static void AddRange(this InternetAddressList list, IEnumerable<EmailMessage.MailAddress> addresses)
{
foreach (var item in addresses)
list.Add(item.ToMailboxAddress());
}
public static MimeMessage ToMailMessage(this EmailMessage message)
{
var subtype = message.IsBodyHtml ? "html" : "plain";
var mailMessage = new MimeMessage()
{
Subject = message.Subject,
Body = new TextPart(subtype) { Text = message.Body }
};
mailMessage.From.Add(message.From.ToMailboxAddress());
mailMessage.To.AddRange(message.To);
mailMessage.Cc.AddRange(message.Cc);
mailMessage.Bcc.AddRange(message.Bcc);
return mailMessage;
}
}
}

View File

@ -0,0 +1,62 @@
using Correo.Abstractions;
using Correo.Abstractions.Extensions;
using Correo.MailKit.Extensions;
using Correo.MailKit.Models;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace Correo.MailKit
{
internal class MailKitSmtpClient : IMailer, IDisposable
{
private readonly IOptions<SmtpClientOptions> _optionsAccessor;
private readonly SmtpClient _smtpClient;
private readonly ILogger<MailKitSmtpClient> _logger;
public MailKitSmtpClient(IOptions<SmtpClientOptions> optionsAccessor, ILogger<MailKitSmtpClient> logger)
{
_optionsAccessor = optionsAccessor;
_logger = logger;
var options = _optionsAccessor.Value;
_smtpClient = new SmtpClient();
_smtpClient.Connect(options.Server, options.Port, options.EnableSsl);
if (options.UseAuthentication)
_smtpClient.Authenticate(options.Authentication.UserName, options.Authentication.Password);
if (options.TrustServer)
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
}
public void Dispose()
{
_smtpClient?.Disconnect(true);
_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.SendAsync(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,19 @@
namespace Correo.MailKit.Models
{
public record SmtpClientOptions
{
public string Server { get; init; }
public int Port { get; init; }
public bool EnableSsl { 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

@ -7,10 +7,10 @@ namespace Correo.SmtpClient
{ {
public static class DependencyInjectionExtensions public static class DependencyInjectionExtensions
{ {
public static void AddSmtpClientServices(this IServiceCollection services, IConfigurationSection configuration) public static void AddNetSmtpClient(this IServiceCollection services, IConfigurationSection configuration)
{ {
services.Configure<SmtpClientOptions>(configuration); services.Configure<SmtpClientOptions>(configuration);
services.AddTransient<IMailer, SmtpClientMailer>(); services.AddTransient<IMailer, NetSmtpClient>();
} }
} }
} }

View File

@ -1,19 +1,15 @@
using Correo.Abstractions; using Correo.Abstractions;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net.Mail; using System.Net.Mail;
namespace Correo.SmtpClient.Extensions namespace Correo.SmtpClient.Extensions
{ {
internal static class ModelExtensions internal static class ModelExtensions
{ {
public static string Log(this IEnumerable<EmailMessage.MailAddress> addresses) private static MailAddress ToMailAddress(this EmailMessage.MailAddress address)
=> 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); => new MailAddress(address.Address, address.DisplayName);
public static void AddRange(this MailAddressCollection collection, IEnumerable<EmailMessage.MailAddress> addresses) private static void AddRange(this MailAddressCollection collection, IEnumerable<EmailMessage.MailAddress> addresses)
{ {
foreach (var item in addresses) foreach (var item in addresses)
collection.Add(item.ToMailAddress()); collection.Add(item.ToMailAddress());

View File

@ -1,4 +1,5 @@
using Correo.Abstractions; using Correo.Abstractions;
using Correo.Abstractions.Extensions;
using Correo.SmtpClient.Extensions; using Correo.SmtpClient.Extensions;
using Correo.SmtpClient.Models; using Correo.SmtpClient.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -10,13 +11,13 @@ using System.Threading.Tasks;
namespace Correo.SmtpClient namespace Correo.SmtpClient
{ {
public class SmtpClientMailer : IMailer, IDisposable internal class NetSmtpClient : IMailer, IDisposable
{ {
private readonly IOptions<SmtpClientOptions> _optionsAccessor; private readonly IOptions<SmtpClientOptions> _optionsAccessor;
private readonly System.Net.Mail.SmtpClient _smtpClient; private readonly System.Net.Mail.SmtpClient _smtpClient;
private readonly ILogger<SmtpClientMailer> _logger; private readonly ILogger<NetSmtpClient> _logger;
public SmtpClientMailer(IOptions<SmtpClientOptions> optionsAccessor, ILogger<SmtpClientMailer> logger) public NetSmtpClient(IOptions<SmtpClientOptions> optionsAccessor, ILogger<NetSmtpClient> logger)
{ {
_optionsAccessor = optionsAccessor; _optionsAccessor = optionsAccessor;
_logger = logger; _logger = logger;

View File

@ -16,6 +16,8 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Correo.Application\Correo.Application.csproj" /> <ProjectReference Include="..\Correo.Application\Correo.Application.csproj" />
<ProjectReference Include="..\Correo.MailKit\Correo.MailKit.csproj" />
<ProjectReference Include="..\Correo.SmtpClient\Correo.SmtpClient.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,30 @@
using Correo.MailKit;
using Correo.SmtpClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Correo.Extensions
{
public static class SmtpExtensions
{
public static void AddSmtpClient(this IServiceCollection services, IConfiguration configuration)
{
var configSection = configuration.GetSection("SmtpClient");
var mediator = configSection.GetValue("Mediator", ".NET");
if (mediator.Equals(".NET", StringComparison.InvariantCultureIgnoreCase))
{
services.AddNetSmtpClient(configSection);
}
else if (mediator.Equals("MailKit", StringComparison.InvariantCultureIgnoreCase))
{
services.AddMailKitSmtpClient(configSection);
}
else
{
throw new NotImplementedException($"SmtpClient:Mediator={mediator} is not implemented.");
}
}
}
}

View File

@ -29,6 +29,9 @@ namespace Correo.Extensions
// Messaging // Messaging
services.AddMessaging(configuration); services.AddMessaging(configuration);
// Add SMTP client
services.AddSmtpClient(configuration);
// Application services // Application services
services.AddApplicationServices(configuration); services.AddApplicationServices(configuration);
} }

View File

@ -30,14 +30,15 @@
"Name": "HomeLab" "Name": "HomeLab"
}, },
"SmtpClient": { "SmtpClient": {
"Mediator": ".NET", //.NET, MailKit
"Server": "smtp.gmail.com", "Server": "smtp.gmail.com",
"Port": "587", "Port": "587",
"UseAuthentication": true,
"Authentication": { "Authentication": {
"UserName": "<account>@gmail.com", "UserName": "<account>@gmail.com",
"Domain": "", "Domain": "",
"Password": "********" "Password": "********"
}, },
"UseAuthentication": true,
"EnableSsl": true, "EnableSsl": true,
"TrustServer": false "TrustServer": false
} }