diff options
| author | ivar <i@oiee.no> | 2023-11-12 00:01:31 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2023-11-12 00:01:31 +0100 |
| commit | a9f9650400ed9e7f40db5ec5fd08a861f4c30902 (patch) | |
| tree | 7df4e6a086acc21a5b47b4fae8bf395f79061f29 /code/api/src/Services/MailService.cs | |
| parent | 854dedead3a3ed987997a0132f527db73b65b0ac (diff) | |
| download | greatoffice-a9f9650400ed9e7f40db5ec5fd08a861f4c30902.tar.xz greatoffice-a9f9650400ed9e7f40db5ec5fd08a861f4c30902.zip | |
Change how config is operated
Diffstat (limited to 'code/api/src/Services/MailService.cs')
| -rw-r--r-- | code/api/src/Services/MailService.cs | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/code/api/src/Services/MailService.cs b/code/api/src/Services/MailService.cs index a6f7db4..4cc5288 100644 --- a/code/api/src/Services/MailService.cs +++ b/code/api/src/Services/MailService.cs @@ -6,11 +6,11 @@ public class MailService private static string _fromEmail; private readonly HttpClient _httpClient; - public MailService(VaultService vaultService, ILogger<MailService> logger, HttpClient httpClient) { - var configuration = vaultService.GetCurrentAppConfiguration(); - _fromEmail = configuration.EMAIL_FROM_ADDRESS; + public MailService(ILogger<MailService> logger, HttpClient httpClient) + { + _fromEmail = Program.AppConfiguration.EMAIL_FROM_ADDRESS; _logger = logger; - httpClient.DefaultRequestHeaders.Add("X-Postmark-Server-Token", configuration.POSTMARK_TOKEN); + httpClient.DefaultRequestHeaders.Add("X-Postmark-Server-Token", Program.AppConfiguration.POSTMARK_TOKEN); _httpClient = httpClient; } @@ -19,35 +19,46 @@ public class MailService /// </summary> /// <param name="message"></param> /// <exception cref="ArgumentException"></exception> - public async Task SendMailAsync(PostmarkEmail message) { - try { - if (message.MessageStream.IsNullOrWhiteSpace()) { + public async Task SendMailAsync(PostmarkEmail message) + { + try + { + if (message.MessageStream.IsNullOrWhiteSpace()) + { message.MessageStream = "outbound"; } - if (message.From.IsNullOrWhiteSpace() && _fromEmail.HasValue()) { + if (message.From.IsNullOrWhiteSpace() && _fromEmail.HasValue()) + { message.From = _fromEmail; - } else { + } + else + { throw new ApplicationException("Not one from-email is available"); } - if (message.To.IsNullOrWhiteSpace()) { + if (message.To.IsNullOrWhiteSpace()) + { throw new ArgumentNullException(nameof(message.To), "A recipient should be specified."); } - if (!message.To.IsValidEmailAddress()) { + if (!message.To.IsValidEmailAddress()) + { throw new ArgumentException(nameof(message.To), "To is not a valid email address"); } - if (message.HtmlBody.IsNullOrWhiteSpace() && message.TextBody.IsNullOrWhiteSpace()) { + if (message.HtmlBody.IsNullOrWhiteSpace() && message.TextBody.IsNullOrWhiteSpace()) + { throw new ArgumentNullException(nameof(message), "Both HtmlBody and TextBody is empty, nothing to send"); } #if DEBUG - _logger.LogInformation("Sending email: {0}", JsonSerializer.Serialize(message, new JsonSerializerOptions() {WriteIndented = true})); + _logger.LogInformation("Sending email: {0}", JsonSerializer.Serialize(message, new JsonSerializerOptions() { WriteIndented = true })); #endif var response = await _httpClient.PostAsJsonAsync("https://api.postmarkapp.com/email", message); _logger.LogInformation("Postmark returned with message: {0}", (await response.Content.ReadFromJsonAsync<PostmarkSendResponse>()).Message); - } catch (Exception e) { + } + catch (Exception e) + { _logger.LogError(e, "A silent exception occured while trying to send an email"); } } @@ -60,7 +71,7 @@ public class MailService public Guid MessageID { get; set; } /// <summary> - /// The message from the API. + /// The message from the API. /// In the event of an error, this message may contain helpful text. /// </summary> public string Message { get; set; } |
