Pull Request 33523 suggestions
Pull Request 33520 suggestions Injected AppSettings instead of using GetEnvironmentVariable at Services level When debugging only app.Services.GetRequiredService<IPCRBService>(); Get ready to use VSCode IDE Align .editorconfig files
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
using FabApprovalWorkerService.Clients;
|
||||
using System.Net.Mail;
|
||||
|
||||
using FabApprovalWorkerService.Clients;
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public interface ISmtpService {
|
||||
@ -15,13 +16,19 @@ public class SmtpService : ISmtpService {
|
||||
private ISmtpClientWrapper _smtpClient;
|
||||
private bool _shouldSendEmail;
|
||||
|
||||
public SmtpService(ILogger<SmtpService> logger, ISmtpClientWrapper smtpClient) {
|
||||
public SmtpService(ILogger<SmtpService> logger,
|
||||
ISmtpClientWrapper smtpClient,
|
||||
AppSettings? appSettings = null) {
|
||||
_logger = logger ??
|
||||
throw new ArgumentNullException("ILogger not injected");
|
||||
_smtpClient = smtpClient ??
|
||||
throw new ArgumentNullException("SmtpClient not injected");
|
||||
if (!Boolean.TryParse(Environment.GetEnvironmentVariable("FabApprovalShouldSendEmail"), out _shouldSendEmail))
|
||||
throw new ArgumentNullException("FabApprovalShouldSendEmail environment variable not found");
|
||||
if (appSettings is not null)
|
||||
_shouldSendEmail = appSettings.ShouldSendEmail;
|
||||
else {
|
||||
if (!bool.TryParse(Environment.GetEnvironmentVariable("FabApprovalShouldSendEmail"), out _shouldSendEmail))
|
||||
throw new ArgumentNullException("FabApprovalShouldSendEmail environment variable not found");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> SendEmail(IEnumerable<MailAddress> recipients,
|
||||
@ -32,7 +39,7 @@ public class SmtpService : ISmtpService {
|
||||
if (ccRecipients is null) throw new ArgumentNullException("ccRecipients cannot be null!");
|
||||
if (subject.IsNullOrEmpty()) throw new ArgumentNullException("subject cannot be null or empty!");
|
||||
if (body.IsNullOrEmpty()) throw new ArgumentNullException("body cannot be null or empty!");
|
||||
|
||||
|
||||
return await Task.Run(() => {
|
||||
int maxRetries = 3;
|
||||
int backoffSeconds = 30;
|
||||
@ -76,4 +83,4 @@ public class SmtpService : ISmtpService {
|
||||
return messageWasSent;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user