diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-11-25 08:32:05 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-11-25 08:32:05 +0100 |
| commit | d852bb69dbce0126a0d8ba660b9931ea7cc062c3 (patch) | |
| tree | 4cca20bc60482c9a7b0b5788ed317e259bcda158 /code/api/src | |
| parent | 43f96fed84f20d875fcfeef8272d88d7a69631cc (diff) | |
| download | greatoffice-d852bb69dbce0126a0d8ba660b9931ea7cc062c3.tar.xz greatoffice-d852bb69dbce0126a0d8ba660b9931ea7cc062c3.zip | |
feat: Suppress and log exceptions when sending email
Diffstat (limited to 'code/api/src')
| -rw-r--r-- | code/api/src/Services/MailService.cs | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/code/api/src/Services/MailService.cs b/code/api/src/Services/MailService.cs index b55b48f..1e565f5 100644 --- a/code/api/src/Services/MailService.cs +++ b/code/api/src/Services/MailService.cs @@ -22,28 +22,31 @@ public class MailService /// </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 - }); + try { + 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); + _logger.LogDebug("SmtpClient was instansiated with the following configuration\n" + configurationString); + smtpClient.Send(message); + } catch (Exception e) { + _logger.LogError(e, "An exception occured while trying to send an email"); + } } }
\ No newline at end of file |
