Added Smtp retry on failure
This commit is contained in:
parent
37f84da148
commit
7d97cd4b36
|
@ -187,8 +187,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
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, SmtpEmailService>(smtp.Key.ToLower(), (provider, key) =>
|
||||||
ActivatorUtilities.CreateInstance<LiveEmailService>(provider,
|
ActivatorUtilities.CreateInstance<SmtpEmailService>(provider,
|
||||||
provider.GetRequiredService<IOptions<EmailConfiguration>>().Value.Smtp[(string)key]));
|
provider.GetRequiredService<IOptions<EmailConfiguration>>().Value.Smtp[(string)key]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
|
|
||||||
namespace Wave.Services;
|
namespace Wave.Services;
|
||||||
|
|
||||||
public class LiveEmailService(ILogger<LiveEmailService> logger, IOptions<EmailConfiguration> emailConfiguration, SmtpConfiguration configuration) : IEmailService {
|
public class SmtpEmailService(ILogger<SmtpEmailService> logger, IOptions<EmailConfiguration> emailConfiguration, SmtpConfiguration configuration) : IEmailService {
|
||||||
private ILogger<LiveEmailService> Logger { get; } = logger;
|
private ILogger<SmtpEmailService> Logger { get; } = logger;
|
||||||
private EmailConfiguration EmailConfiguration { get; } = emailConfiguration.Value;
|
private EmailConfiguration EmailConfiguration { get; } = emailConfiguration.Value;
|
||||||
private SmtpConfiguration Configuration { get; } = configuration;
|
private SmtpConfiguration Configuration { get; } = configuration;
|
||||||
|
|
||||||
|
@ -44,7 +44,6 @@ public class LiveEmailService(ILogger<LiveEmailService> logger, IOptions<EmailCo
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ValueTask SendEmailAsync(IEmail email) {
|
public async ValueTask SendEmailAsync(IEmail email) {
|
||||||
try {
|
|
||||||
if (Client is null) throw new ApplicationException("Not connected.");
|
if (Client is null) throw new ApplicationException("Not connected.");
|
||||||
|
|
||||||
var message = new MimeMessage {
|
var message = new MimeMessage {
|
||||||
|
@ -64,15 +63,21 @@ public class LiveEmailService(ILogger<LiveEmailService> logger, IOptions<EmailCo
|
||||||
message.Headers.Add(id, value);
|
message.Headers.Add(id, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int retryCount = 0;
|
||||||
|
while (retryCount < 3) {
|
||||||
try {
|
try {
|
||||||
await Client.SendAsync(message);
|
await Client.SendAsync(message);
|
||||||
Logger.LogInformation("Successfully send mail to {email} (subject: {subject}).",
|
Logger.LogInformation("Successfully send mail to {email} (subject: {subject}).",
|
||||||
email.ReceiverEmail, email.Subject);
|
email.ReceiverEmail, email.Subject);
|
||||||
|
return;
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new EmailNotSendException("Failed Email send.", ex);
|
retryCount++;
|
||||||
|
Logger.LogWarning(ex, "Error sending E-Mail to {email}. Try: {RetryCount}.",
|
||||||
|
email.ReceiverEmail, retryCount);
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
|
||||||
Logger.LogError(ex, "Error sending E-Mail");
|
|
||||||
}
|
}
|
||||||
|
// TODO enqueue for re-sending or throw exception if applicable and handle hard bounce
|
||||||
|
// throw new EmailNotSendException();
|
||||||
|
Logger.LogError("Giving up");
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue