Configuration structure changes

master
Tudor Stanciu 2023-01-21 04:25:44 +02:00
parent 35124b515d
commit 6931151665
5 changed files with 36 additions and 12 deletions

View File

@ -7,7 +7,7 @@ namespace Correo.MailKit
{ {
public static class DependencyInjectionExtensions public static class DependencyInjectionExtensions
{ {
public static void AddMailKitSmtpClient(this IServiceCollection services, IConfigurationSection configuration) public static void AddMailKitSmtpClient(this IServiceCollection services, IConfiguration configuration)
{ {
services.Configure<SmtpClientOptions>(configuration); services.Configure<SmtpClientOptions>(configuration);
services.AddTransient<IMailer, MailKitSmtpClient>(); services.AddTransient<IMailer, MailKitSmtpClient>();

View File

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

View File

@ -0,0 +1,10 @@
using System;
namespace Correo.Extensions
{
internal static class DataTypeExtensions
{
public static bool EqualsIgnoreCase(this string value1, string value2)
=> value1.Equals(value2, StringComparison.InvariantCultureIgnoreCase);
}
}

View File

@ -12,20 +12,16 @@ namespace Correo.Extensions
{ {
public static void AddSmtpService(this IServiceCollection services, IConfiguration configuration) public static void AddSmtpService(this IServiceCollection services, IConfiguration configuration)
{ {
var mediator = configuration.GetValue("SmtpMediator", ".NET"); var mediator = configuration.GetValue("SmtpMediator", "SmtpClient");
if (mediator.Equals(".NET", StringComparison.InvariantCultureIgnoreCase)) if (mediator.EqualsIgnoreCase("SmtpClient"))
{ {
services.AddNetSmtpClient(configuration.GetSection("SmtpClient")); services.AddSmtpClient(configuration.GetSection("SmtpClient"));
} }
else if (mediator.Equals("MailKit", StringComparison.InvariantCultureIgnoreCase)) else if (mediator.EqualsIgnoreCase("SendGrid"))
{
services.AddMailKitSmtpClient(configuration.GetSection("SmtpClient"));
}
else if (mediator.Equals("SendGrid", StringComparison.InvariantCultureIgnoreCase))
{ {
services.AddSendGridService(configuration.GetSection("SendGrid")); services.AddSendGridService(configuration.GetSection("SendGrid"));
} }
else if (mediator.Equals("Mailgun", StringComparison.InvariantCultureIgnoreCase)) else if (mediator.EqualsIgnoreCase("Mailgun"))
{ {
services.AddMailgunService(configuration.GetSection("Mailgun")); services.AddMailgunService(configuration.GetSection("Mailgun"));
} }
@ -34,5 +30,22 @@ namespace Correo.Extensions
throw new NotImplementedException($"SmtpMediator '{mediator}' is not implemented."); throw new NotImplementedException($"SmtpMediator '{mediator}' is not implemented.");
} }
} }
private static void AddSmtpClient(this IServiceCollection services, IConfiguration configuration)
{
var package = configuration.GetValue("Package", ".NET");
if (package.EqualsIgnoreCase(".NET"))
{
services.AddNetSmtpClient(configuration);
}
else if (package.EqualsIgnoreCase("MailKit"))
{
services.AddMailKitSmtpClient(configuration);
}
else
{
throw new NotImplementedException($"SmtpClient:Package '{package}' is not implemented.");
}
}
} }
} }

View File

@ -29,8 +29,9 @@
"Address": "noreply@homelab.com", "Address": "noreply@homelab.com",
"Name": "HomeLab" "Name": "HomeLab"
}, },
"SmtpMediator": ".NET", //.NET, MailKit, SendGrid, Mailgun "SmtpMediator": "SmtpClient", //SmtpClient, SendGrid, Mailgun
"SmtpClient": { "SmtpClient": {
"Package": "MailKit", //.NET, MailKit
"Server": "smtp.gmail.com", "Server": "smtp.gmail.com",
"Port": "587", "Port": "587",
"UseAuthentication": true, "UseAuthentication": true,