From a640703f2da8815dc26ad1600a6f206be1624379 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Wed, 1 Jun 2022 22:10:32 +0200 Subject: feat: Initial after clean slate --- server/src/Services/MailService.cs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/src/Services/MailService.cs (limited to 'server/src/Services/MailService.cs') 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 _logger; + private static string _emailHost; + private static int _emailPort; + private static string _emailUser; + private static string _emailPassword; + + /// + /// Provides methods to send email. + /// + /// + /// + public MailService(IConfiguration configuration, ILogger logger) { + _logger = logger; + _emailHost = configuration.GetValue(AppEnvironmentVariables.SMTP_HOST); + _emailPort = configuration.GetValue(AppEnvironmentVariables.SMTP_PORT); + _emailUser = configuration.GetValue(AppEnvironmentVariables.SMTP_USER); + _emailPassword = configuration.GetValue(AppEnvironmentVariables.SMTP_PASSWORD); + } + + /// + /// Send an email. + /// + /// + 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); + } +} -- cgit v1.3