From 2862c3892e32ca0ef21705f21099d0a8c74f35dc Mon Sep 17 00:00:00 2001 From: Demis Bellot Date: Thu, 5 Sep 2024 10:32:44 +0800 Subject: [PATCH] Add Configure.Smtp.cs --- MyApp/Configure.Smtp.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 MyApp/Configure.Smtp.cs diff --git a/MyApp/Configure.Smtp.cs b/MyApp/Configure.Smtp.cs new file mode 100644 index 0000000..6cf03b1 --- /dev/null +++ b/MyApp/Configure.Smtp.cs @@ -0,0 +1,31 @@ +using MyApp.ServiceInterface; + +[assembly: HostingStartup(typeof(MyApp.ConfigureSmtp))] + +namespace MyApp; + +public class ConfigureSmtp : IHostingStartup +{ + public void Configure(IWebHostBuilder builder) => builder + .ConfigureServices((context,services) => { + var smtpConfig = context.Configuration.GetSection(nameof(SmtpConfig))?.Get(); + if (smtpConfig is not null) + { + services.AddSingleton(smtpConfig); + } + }) + .ConfigureAppHost(appHost => { + // Check if SMTP is configured + var smtpConfig = appHost.TryResolve(); + var log = appHost.GetApplicationServices().GetRequiredService>(); + // Log if missing + if (smtpConfig is null) + { + log.LogWarning("SMTP is not configured, please configure SMTP to enable sending emails"); + } + else + { + log.LogWarning("SMTP is configured with <{SmtpConfigFromEmail}> {SmtpConfigFromName}", smtpConfig.FromEmail, smtpConfig.FromName); + } + }); +} \ No newline at end of file