aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Services/MailService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'code/api/src/Services/MailService.cs')
-rw-r--r--code/api/src/Services/MailService.cs144
1 files changed, 114 insertions, 30 deletions
diff --git a/code/api/src/Services/MailService.cs b/code/api/src/Services/MailService.cs
index 1e565f5..6073f6e 100644
--- a/code/api/src/Services/MailService.cs
+++ b/code/api/src/Services/MailService.cs
@@ -3,50 +3,134 @@ 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;
+ private static string _postmarkToken;
+ private static string _fromEmail;
+ private readonly HttpClient _httpClient;
- public MailService(VaultService vaultService, ILogger<MailService> logger) {
+ public MailService(VaultService vaultService, ILogger<MailService> logger, HttpClient httpClient) {
var configuration = vaultService.GetCurrentAppConfiguration();
+ _postmarkToken = configuration.POSTMARK_TOKEN;
+ _fromEmail = configuration.EMAIL_FROM_ADDRESS;
_logger = logger;
- _emailHost = configuration.SMTP_HOST;
- _emailPort = Convert.ToInt32(configuration.SMTP_PORT);
- _emailUser = configuration.SMTP_USER;
- _emailPassword = configuration.SMTP_PASSWORD;
+ httpClient.DefaultRequestHeaders.Add("X-Postmark-Server-Token", _postmarkToken);
+ _httpClient = httpClient;
}
/// <summary>
/// Send an email.
/// </summary>
/// <param name="message"></param>
- public void SendMail(MailMessage message) {
+ public async Task SendMail(PostmarkEmail message) {
try {
- using var smtpClient = new SmtpClient {
- Host = _emailHost,
- EnableSsl = _emailPort == 587,
- Port = _emailPort,
- Credentials = new NetworkCredential {
- UserName = _emailUser,
- Password = _emailPassword,
+ if (message.MessageStream.IsNullOrWhiteSpace()) {
+ message.MessageStream = "outbound";
+ }
+
+ if (message.From.IsNullOrWhiteSpace() && _fromEmail.HasValue()) {
+ message.From = _fromEmail;
+ } else {
+ throw new ApplicationException("Not one from-email is available");
+ }
+
+ if (message.To.IsNullOrWhiteSpace()) {
+ throw new ArgumentNullException(nameof(message.To), "A recipient should be specified.");
+ }
+
+ if (!message.To.IsValidEmailAddress()) {
+ throw new ArgumentException(nameof(message.To), "To is not a valid email address");
+ }
+
+ if (message.HtmlBody.IsNullOrWhiteSpace() && message.TextBody.IsNullOrWhiteSpace()) {
+ throw new ArgumentNullException(nameof(message), "Both HtmlBody and TextBody is empty, nothing to send");
+ }
+
+ using var client = new HttpClient() {
+ DefaultRequestHeaders = {
+ {"X-Postmark-Server-Token", _postmarkToken},
}
};
- 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);
+ // TODO: Log response if unsuccessful
+ await client.PostAsJsonAsync("https://api.postmarkapp.com/email", message);
} catch (Exception e) {
_logger.LogError(e, "An exception occured while trying to send an email");
}
}
+
+ private class PostmarkSendResponse
+ {
+ /// <summary>
+ /// The Message ID returned from Postmark.
+ /// </summary>
+ public Guid MessageID { get; set; }
+
+ /// <summary>
+ /// The message from the API.
+ /// In the event of an error, this message may contain helpful text.
+ /// </summary>
+ public string Message { get; set; }
+
+ /// <summary>
+ /// The time the request was received by Postmark.
+ /// </summary>
+ public DateTime SubmittedAt { get; set; }
+
+ /// <summary>
+ /// The recipient of the submitted request.
+ /// </summary>
+ public string To { get; set; }
+
+ /// <summary>
+ /// The error code returned from Postmark.
+ /// This does not map to HTTP status codes.
+ /// </summary>
+ public int ErrorCode { get; set; }
+ }
+
+ public class PostmarkEmail
+ {
+ /// <summary>
+ /// The message subject line.
+ /// </summary>
+ public string Subject { get; set; }
+
+ /// <summary>
+ /// The message body, if the message contains
+ /// </summary>
+ public string HtmlBody { get; set; }
+
+ /// <summary>
+ /// The message body, if the message is plain text.
+ /// </summary>
+ public string TextBody { get; set; }
+
+ /// <summary>
+ /// The message stream used to send this message.
+ /// </summary>
+ public string MessageStream { get; set; }
+
+ /// <summary>
+ /// The sender's email address.
+ /// </summary>
+ public string From { get; set; }
+
+ /// <summary>
+ /// Any recipients. Separate multiple recipients with a comma.
+ /// </summary>
+ public string To { get; set; }
+
+ /// <summary>
+ /// Any CC recipients. Separate multiple recipients with a comma.
+ /// </summary>
+ public string Cc { get; set; }
+
+ /// <summary>
+ /// Any BCC recipients. Separate multiple recipients with a comma.
+ /// </summary>
+ public string Bcc { get; set; }
+
+ /// <summary>
+ /// The email address to reply to. This is optional.
+ /// </summary>
+ public string ReplyTo { get; set; }
+ }
} \ No newline at end of file