diff options
Diffstat (limited to 'server/src/Services/MailService.cs')
| -rw-r--r-- | server/src/Services/MailService.cs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/server/src/Services/MailService.cs b/server/src/Services/MailService.cs new file mode 100644 index 0000000..b271de4 --- /dev/null +++ b/server/src/Services/MailService.cs @@ -0,0 +1,52 @@ +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; + + /// <summary> + /// Provides methods to send email. + /// </summary> + /// <param name="configuration"></param> + /// <param name="logger"></param> + public MailService(IConfiguration configuration, ILogger<MailService> logger) { + _logger = logger; + _emailHost = configuration.GetValue<string>(AppEnvironmentVariables.SMTP_HOST); + _emailPort = configuration.GetValue<int>(AppEnvironmentVariables.SMTP_PORT); + _emailUser = configuration.GetValue<string>(AppEnvironmentVariables.SMTP_USER); + _emailPassword = configuration.GetValue<string>(AppEnvironmentVariables.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 { + Host = smtpClient.Host, + EnableSsl = smtpClient.EnableSsl, + Port = 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); + } +} |
