Don't send emails in dev

This commit is contained in:
Chase Tucker 2024-03-29 15:10:35 -07:00
parent a86fbbbea0
commit fd23214ef7

View File

@ -13,12 +13,15 @@ public interface ISmtpService {
public class SmtpService : ISmtpService { public class SmtpService : ISmtpService {
private ILogger<SmtpService> _logger; private ILogger<SmtpService> _logger;
private ISmtpClientWrapper _smtpClient; private ISmtpClientWrapper _smtpClient;
private string _environmentName;
public SmtpService(ILogger<SmtpService> logger, ISmtpClientWrapper smtpClient) { public SmtpService(ILogger<SmtpService> logger, ISmtpClientWrapper smtpClient) {
_logger = logger ?? _logger = logger ??
throw new ArgumentNullException("ILogger not injected"); throw new ArgumentNullException("ILogger not injected");
_smtpClient = smtpClient ?? _smtpClient = smtpClient ??
throw new ArgumentNullException("SmtpClient not injected"); throw new ArgumentNullException("SmtpClient not injected");
_environmentName = Environment.GetEnvironmentVariable("FabApprovalEnvironmentName") ??
throw new ArgumentNullException("FabApprovalEnvironmentName environment variable not found");
} }
public async Task<bool> SendEmail(IEnumerable<MailAddress> recipients, public async Task<bool> SendEmail(IEnumerable<MailAddress> recipients,
@ -37,29 +40,33 @@ public class SmtpService : ISmtpService {
bool messageWasSent = false; bool messageWasSent = false;
try { try {
int remainingRetries = maxRetries; if (!_environmentName.ToLower().Equals("development")) {
while (!messageWasSent && remainingRetries > 0) { int remainingRetries = maxRetries;
try { while (!messageWasSent && remainingRetries > 0) {
Task.Delay((maxRetries - remainingRetries--) * backoffSeconds * 1000); try {
Task.Delay((maxRetries - remainingRetries--) * backoffSeconds * 1000);
_logger.LogInformation($"Attempting to send notification. Remaining retries: {remainingRetries}"); _logger.LogInformation($"Attempting to send notification. Remaining retries: {remainingRetries}");
MailMessage msg = new MailMessage(); MailMessage msg = new MailMessage();
msg.IsBodyHtml = true; msg.IsBodyHtml = true;
msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval"); msg.From = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
msg.Sender = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval"); msg.Sender = new MailAddress("MesaFabApproval@infineon.com", "Mesa Fab Approval");
foreach (MailAddress recipient in recipients) msg.To.Add(recipient); foreach (MailAddress recipient in recipients) msg.To.Add(recipient);
msg.Bcc.Add("chase.tucker@infineon.com"); msg.Bcc.Add("chase.tucker@infineon.com");
foreach (MailAddress ccRecipient in ccRecipients) msg.CC.Add(ccRecipient); foreach (MailAddress ccRecipient in ccRecipients) msg.CC.Add(ccRecipient);
msg.Subject = subject; msg.Subject = subject;
msg.Body = body; msg.Body = body;
_smtpClient.Send(msg); _smtpClient.Send(msg);
messageWasSent = true; messageWasSent = true;
} catch (Exception ex) { } catch (Exception ex) {
_logger.LogError($"Message not sent successfully. Exception: {ex.Message}"); _logger.LogError($"Message not sent successfully. Exception: {ex.Message}");
}
} }
} else {
messageWasSent = true;
} }
} catch (Exception ex) { } catch (Exception ex) {
_logger.LogError($"An exception occurred when attempting to send notification. Exception: {ex.Message}"); _logger.LogError($"An exception occurred when attempting to send notification. Exception: {ex.Message}");