fixed Issues related to not having email configured

This commit is contained in:
Mia Rose Winter 2024-03-16 12:32:20 +01:00
parent eaa30ff6b1
commit 075c5ecc9c
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 11 additions and 2 deletions

View file

@ -178,14 +178,14 @@
var emailConfig = builder.Configuration.GetSection("Email").Get<EmailConfiguration>(); var emailConfig = builder.Configuration.GetSection("Email").Get<EmailConfiguration>();
builder.Services.Configure<EmailConfiguration>(builder.Configuration.GetSection("Email")); builder.Services.Configure<EmailConfiguration>(builder.Configuration.GetSection("Email"));
builder.Services.AddSingleton<EmailTemplateService>();
builder.Services.AddScoped<EmailFactory>();
if (emailConfig?.Smtp.Count > 0) { if (emailConfig?.Smtp.Count > 0) {
if (string.IsNullOrWhiteSpace(emailConfig.SenderEmail)) { if (string.IsNullOrWhiteSpace(emailConfig.SenderEmail)) {
throw new ApplicationException( throw new ApplicationException(
"Email providers have been configured, but no SenderEmail. " + "Email providers have been configured, but no SenderEmail. " +
"Please provider the sender email address used for email distribution."); "Please provider the sender email address used for email distribution.");
} }
builder.Services.AddSingleton<EmailTemplateService>();
builder.Services.AddScoped<EmailFactory>();
foreach (var smtp in emailConfig.Smtp) { foreach (var smtp in emailConfig.Smtp) {
builder.Services.AddKeyedScoped<IEmailService, LiveEmailService>(smtp.Key.ToLower(), (provider, key) => builder.Services.AddKeyedScoped<IEmailService, LiveEmailService>(smtp.Key.ToLower(), (provider, key) =>
@ -210,6 +210,7 @@
"Disable email subscriptions or provide the mail provider for bulk sending"); "Disable email subscriptions or provide the mail provider for bulk sending");
} }
} else { } else {
builder.Services.AddSingleton<IEmailService, NoOpEmailService>();
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>(); builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSender>();
logMessages.Add("No email provider configured."); logMessages.Add("No email provider configured.");
} }

View file

@ -6,3 +6,11 @@ public interface IEmailService : IAsyncDisposable {
ValueTask SendEmailAsync(IEmail email); ValueTask SendEmailAsync(IEmail email);
} }
public sealed class NoOpEmailService : IEmailService {
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
public ValueTask ConnectAsync(CancellationToken cancellation) => ValueTask.CompletedTask;
public ValueTask DisconnectAsync(CancellationToken cancellation) => ValueTask.CompletedTask;
public ValueTask SendEmailAsync(IEmail email) => ValueTask.CompletedTask;
}