Added MailKit
parent
dc5ce5e96a
commit
31dfbe1ccf
17
Correo.sln
17
Correo.sln
|
@ -18,15 +18,17 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution
|
|||
README.md = README.md
|
||||
EndProjectSection
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
Global
|
||||
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}.Release|Any CPU.ActiveCfg = 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
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -69,6 +75,7 @@ Global
|
|||
{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}
|
||||
{325B77E1-D752-4578-8BF7-793905C38DCD} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D}
|
||||
|
|
|
@ -8,5 +8,6 @@
|
|||
<AutoMapperExtensionsPackageVersion>12.0.0</AutoMapperExtensionsPackageVersion>
|
||||
<MediatRPackageVersion>9.0.0</MediatRPackageVersion>
|
||||
<NBBPackageVersion>6.0.30</NBBPackageVersion>
|
||||
<MailKitPackageVersion>3.4.3</MailKitPackageVersion>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -14,7 +14,6 @@
|
|||
<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>
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using Correo.Domain.Models;
|
||||
using Correo.SmtpClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
@ -10,7 +9,6 @@ namespace Correo.Application
|
|||
public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.Configure<DefaultSender>(configuration.GetSection("DefaultSender"));
|
||||
services.AddSmtpClientServices(configuration.GetSection("SmtpClient"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -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>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()};");
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,10 +7,10 @@ namespace Correo.SmtpClient
|
|||
{
|
||||
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.AddTransient<IMailer, SmtpClientMailer>();
|
||||
services.AddTransient<IMailer, NetSmtpClient>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +1,15 @@
|
|||
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)
|
||||
private static MailAddress ToMailAddress(this EmailMessage.MailAddress address)
|
||||
=> 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)
|
||||
collection.Add(item.ToMailAddress());
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Correo.Abstractions;
|
||||
using Correo.Abstractions.Extensions;
|
||||
using Correo.SmtpClient.Extensions;
|
||||
using Correo.SmtpClient.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
@ -10,13 +11,13 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace Correo.SmtpClient
|
||||
{
|
||||
public class SmtpClientMailer : IMailer, IDisposable
|
||||
internal class NetSmtpClient : IMailer, IDisposable
|
||||
{
|
||||
private readonly IOptions<SmtpClientOptions> _optionsAccessor;
|
||||
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;
|
||||
_logger = logger;
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Correo.Application\Correo.Application.csproj" />
|
||||
<ProjectReference Include="..\Correo.MailKit\Correo.MailKit.csproj" />
|
||||
<ProjectReference Include="..\Correo.SmtpClient\Correo.SmtpClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,6 +29,9 @@ namespace Correo.Extensions
|
|||
// Messaging
|
||||
services.AddMessaging(configuration);
|
||||
|
||||
// Add SMTP client
|
||||
services.AddSmtpClient(configuration);
|
||||
|
||||
// Application services
|
||||
services.AddApplicationServices(configuration);
|
||||
}
|
||||
|
|
|
@ -30,14 +30,15 @@
|
|||
"Name": "HomeLab"
|
||||
},
|
||||
"SmtpClient": {
|
||||
"Mediator": ".NET", //.NET, MailKit
|
||||
"Server": "smtp.gmail.com",
|
||||
"Port": "587",
|
||||
"UseAuthentication": true,
|
||||
"Authentication": {
|
||||
"UserName": "<account>@gmail.com",
|
||||
"Domain": "",
|
||||
"Password": "********"
|
||||
},
|
||||
"UseAuthentication": true,
|
||||
"EnableSsl": true,
|
||||
"TrustServer": false
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue