MRB webassembly
This commit is contained in:
79
MesaFabApproval.API/Services/SmtpService.cs
Normal file
79
MesaFabApproval.API/Services/SmtpService.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using MesaFabApproval.API.Clients;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace MesaFabApproval.API.Services;
|
||||
|
||||
public interface ISmtpService {
|
||||
Task<bool> SendEmail(IEnumerable<MailAddress> recipients, IEnumerable<MailAddress> ccRecipients, string subject, string body);
|
||||
}
|
||||
|
||||
public class SmtpService : ISmtpService {
|
||||
private readonly ILogger<SmtpService> _logger;
|
||||
private readonly ISmtpClientWrapper _smtpClient;
|
||||
private readonly bool _shouldSendEmail;
|
||||
|
||||
public SmtpService(ILogger<SmtpService> logger, ISmtpClientWrapper smtpClient) {
|
||||
_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");
|
||||
}
|
||||
|
||||
public async Task<bool> SendEmail(IEnumerable<MailAddress> recipients,
|
||||
IEnumerable<MailAddress> ccRecipients,
|
||||
string subject,
|
||||
string body) {
|
||||
if (recipients.IsNullOrEmpty()) throw new ArgumentNullException("recipients cannot be null or empty!");
|
||||
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;
|
||||
|
||||
bool messageWasSent = false;
|
||||
|
||||
try {
|
||||
if (_shouldSendEmail) {
|
||||
int remainingRetries = maxRetries;
|
||||
while (!messageWasSent && remainingRetries > 0) {
|
||||
try {
|
||||
Task.Delay((maxRetries - remainingRetries--) * backoffSeconds * 1000);
|
||||
|
||||
_logger.LogInformation($"Attempting to send notification. Remaining retries: {remainingRetries}");
|
||||
|
||||
MailMessage msg = new MailMessage();
|
||||
msg.IsBodyHtml = true;
|
||||
msg.From = 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);
|
||||
msg.Bcc.Add("chase.tucker@infineon.com");
|
||||
foreach (MailAddress ccRecipient in ccRecipients) msg.CC.Add(ccRecipient);
|
||||
msg.Subject = subject;
|
||||
msg.Body = body;
|
||||
|
||||
_smtpClient.Send(msg);
|
||||
|
||||
messageWasSent = true;
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Message not sent successfully. Exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_logger.LogInformation("Not sending email per local configuration");
|
||||
messageWasSent = true;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"An exception occurred when attempting to send notification. Exception: {ex.Message}");
|
||||
}
|
||||
|
||||
return messageWasSent;
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user