diff options
Diffstat (limited to 'code/api/src/Services/MailService.cs')
| -rw-r--r-- | code/api/src/Services/MailService.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/code/api/src/Services/MailService.cs b/code/api/src/Services/MailService.cs new file mode 100644 index 0000000..c08cb84 --- /dev/null +++ b/code/api/src/Services/MailService.cs @@ -0,0 +1,49 @@ +namespace IOL.GreatOffice.Api.Services; + +public class MailService +{ + private readonly ILogger<MailService> _logger; + private static string _emailHost; + private static int _emailPort; + private static string _emailUser; + private static string _emailPassword; + + public MailService(VaultService vaultService, ILogger<MailService> logger) { + var configuration = vaultService.GetCurrentAppConfiguration(); + _logger = logger; + _emailHost = configuration.SMTP_HOST; + _emailPort = Convert.ToInt32(configuration.SMTP_PORT); + _emailUser = configuration.SMTP_USER; + _emailPassword = configuration.SMTP_PASSWORD; + } + + /// <summary> + /// Send an email. + /// </summary> + /// <param name="message"></param> + public void SendMail(MailMessage message) { + using var smtpClient = new SmtpClient { + Host = _emailHost, + EnableSsl = _emailPort == 587, + Port = _emailPort, + Credentials = new NetworkCredential { + UserName = _emailUser, + Password = _emailPassword, + } + }; + var configurationString = JsonSerializer.Serialize(new { + smtpClient.Host, + smtpClient.EnableSsl, + smtpClient.Port, + UserName = _emailUser.HasValue() ? "**REDACTED**" : "**MISSING**", + Password = _emailPassword.HasValue() ? "**REDACTED**" : "**MISSING**", + }, + new JsonSerializerOptions { + WriteIndented = true + }); + + _logger.LogDebug("SmtpClient was instansiated with the following configuration\n" + configurationString); + + smtpClient.Send(message); + } +}
\ No newline at end of file |
