From 2017e1758a711c68df0f53fcff3a122fd3e1431c Mon Sep 17 00:00:00 2001 From: Panagiotis <400362+panosru@users.noreply.github.com> Date: Mon, 19 Feb 2024 16:37:18 +0200 Subject: [PATCH] Add Email Service --- Domains/Shared/Application/Application.csproj | 1 + .../Infrastructure/Infrastructure.csproj | 1 + .../Presentation/ServiceConfiguration.cs | 1 + .../Presentation/ServiceExtensions/Email.cs | 48 +++++++++++++++++++ .../Presentation/appsettings.Development.yaml | 3 ++ 5 files changed, 54 insertions(+) create mode 100644 Hosts/RestApi/Presentation/ServiceExtensions/Email.cs diff --git a/Domains/Shared/Application/Application.csproj b/Domains/Shared/Application/Application.csproj index 282d6440..3b9c5f53 100644 --- a/Domains/Shared/Application/Application.csproj +++ b/Domains/Shared/Application/Application.csproj @@ -7,6 +7,7 @@ + diff --git a/Domains/Shared/Infrastructure/Infrastructure.csproj b/Domains/Shared/Infrastructure/Infrastructure.csproj index e31bfc0d..847790cd 100644 --- a/Domains/Shared/Infrastructure/Infrastructure.csproj +++ b/Domains/Shared/Infrastructure/Infrastructure.csproj @@ -7,6 +7,7 @@ + diff --git a/Hosts/RestApi/Presentation/ServiceConfiguration.cs b/Hosts/RestApi/Presentation/ServiceConfiguration.cs index 7d7f45a5..69add496 100644 --- a/Hosts/RestApi/Presentation/ServiceConfiguration.cs +++ b/Hosts/RestApi/Presentation/ServiceConfiguration.cs @@ -22,6 +22,7 @@ public static void ConfigureServices( services.AddSingleton(configuration); DependencyInjectionRegistry.CurrentEnvironment = environment; + services.AddEmailService(configuration); services.AddAutoMapperServices(); services.AddValidatorServices(); services.AddDataProtectionServices(); diff --git a/Hosts/RestApi/Presentation/ServiceExtensions/Email.cs b/Hosts/RestApi/Presentation/ServiceExtensions/Email.cs new file mode 100644 index 00000000..16e610e1 --- /dev/null +++ b/Hosts/RestApi/Presentation/ServiceExtensions/Email.cs @@ -0,0 +1,48 @@ +using Aviant.Application.Email; +using Aviant.Infrastructure.Email; + +namespace CleanDDDArchitecture.Hosts.RestApi.Presentation.ServiceExtensions; + +/// +/// Email service extension +/// +public static class Email +{ + /// + /// Add email service + /// + /// + /// + /// + public static IServiceCollection AddEmailService( + this IServiceCollection services, + IConfiguration configuration) + { + // Get the configuration properties + var smtpHost = configuration["EmailSettings:SmtpHost"]; + var smtpPort = int.Parse(configuration["EmailSettings:SmtpPort"]); + var enableSsl = bool.Parse(configuration["EmailSettings:EnableSsl"]); + var smtpUsername = configuration["EmailSettings:SmtpUsername"]; + var smtpPassword = configuration["EmailSettings:SmtpPassword"]; + var fromName = configuration["EmailSettings:FromName"]; + var fromEmail = configuration["EmailSettings:FromEmail"]; + + services.AddSingleton( + provider => new SmtpClientFactory( + smtpHost, + smtpPort, + enableSsl, + smtpUsername, + smtpPassword)); + + services.AddTransient( + provider => + { + var smtpClientFactory = provider.GetRequiredService(); + + return new EmailService(smtpClientFactory, fromName, fromEmail); + }); + + return services; + } +} diff --git a/Hosts/RestApi/Presentation/appsettings.Development.yaml b/Hosts/RestApi/Presentation/appsettings.Development.yaml index bcef9e7f..11e371b7 100644 --- a/Hosts/RestApi/Presentation/appsettings.Development.yaml +++ b/Hosts/RestApi/Presentation/appsettings.Development.yaml @@ -1,4 +1,7 @@ --- +EmailSettings: + FromName: "Clean DDD Architecture" + FromEmail: "no-reply@cleanddd.com" Serilog: Using: