1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
namespace IOL.GreatOffice.Api.Services;
public class MailService
{
private readonly ILogger<MailService> _logger;
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;
_logger = logger;
httpClient.DefaultRequestHeaders.Add("X-Postmark-Server-Token", configuration.POSTMARK_TOKEN);
_httpClient = httpClient;
}
/// <summary>
/// Send an email.
/// </summary>
/// <param name="message"></param>
public async Task SendMail(PostmarkEmail message) {
try {
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");
}
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) {
_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; }
}
}
|