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,35 +44,40 @@ 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 {
|
From = { new MailboxAddress(EmailConfiguration.SenderName, EmailConfiguration.SenderEmail) },
|
||||||
From = { new MailboxAddress(EmailConfiguration.SenderName, EmailConfiguration.SenderEmail) },
|
To = { new MailboxAddress(email.ReceiverName, email.ReceiverEmail) },
|
||||||
To = { new MailboxAddress(email.ReceiverName, email.ReceiverEmail) },
|
Subject = email.Subject
|
||||||
Subject = email.Subject
|
};
|
||||||
};
|
var builder = new BodyBuilder {
|
||||||
var builder = new BodyBuilder {
|
HtmlBody = email.ContentHtml,
|
||||||
HtmlBody = email.ContentHtml,
|
TextBody = email.ContentPlain
|
||||||
TextBody = email.ContentPlain
|
};
|
||||||
};
|
message.Body = builder.ToMessageBody();
|
||||||
message.Body = builder.ToMessageBody();
|
foreach ((string id, string value) in email.Headers) {
|
||||||
foreach ((string id, string value) in email.Headers) {
|
if (id == HeaderId.ListUnsubscribe.ToHeaderName()) {
|
||||||
if (id == HeaderId.ListUnsubscribe.ToHeaderName()) {
|
message.Headers.Add(HeaderId.ListId, $"<mailto:{EmailConfiguration.ServiceEmail ?? EmailConfiguration.SenderEmail}>");
|
||||||
message.Headers.Add(HeaderId.ListId, $"<mailto:{EmailConfiguration.ServiceEmail ?? EmailConfiguration.SenderEmail}>");
|
|
||||||
}
|
|
||||||
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