-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathMailPaceSender.cs
More file actions
143 lines (120 loc) · 4.75 KB
/
MailPaceSender.cs
File metadata and controls
143 lines (120 loc) · 4.75 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using Newtonsoft.Json;
namespace FluentEmail.MailPace;
public class MailPaceSender : ISender, IDisposable
{
private readonly HttpClient _httpClient;
public MailPaceSender(string serverToken)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("MailPace-Server-Token", serverToken);
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public SendResponse Send(IFluentEmail email, CancellationToken? token = null) =>
SendAsync(email, token).GetAwaiter().GetResult();
public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token = null)
{
var sendRequest = BuildSendRequestFor(email);
var content = new StringContent(JsonConvert.SerializeObject(sendRequest), Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("https://app.mailpace.com/api/v1/send", content)
.ConfigureAwait(false);
var mailPaceResponse = await TryOrNull(async () => JsonConvert.DeserializeObject<MailPaceResponse>(
await response.Content.ReadAsStringAsync())) ?? new MailPaceResponse();
if (response.IsSuccessStatusCode)
{
return new SendResponse { MessageId = mailPaceResponse.Id };
}
else
{
var result = new SendResponse();
if (!string.IsNullOrEmpty(mailPaceResponse.Error))
{
result.ErrorMessages.Add(mailPaceResponse.Error);
}
if (mailPaceResponse.Errors != null && mailPaceResponse.Errors.Count != 0)
{
result.ErrorMessages.AddRange(mailPaceResponse.Errors
.Select(it => $"{it.Key}: {string.Join("; ", it.Value)}"));
}
if (!result.ErrorMessages.Any())
{
result.ErrorMessages.Add(response.ReasonPhrase ?? "An unknown error has occurred.");
}
return result;
}
}
private static MailPaceSendRequest BuildSendRequestFor(IFluentEmail email)
{
var sendRequest = new MailPaceSendRequest
{
From = $"{email.Data.FromAddress.Name} <{email.Data.FromAddress.EmailAddress}>",
To = string.Join(",", email.Data.ToAddresses.Select(it => !string.IsNullOrEmpty(it.Name) ? $"{it.Name} <{it.EmailAddress}>" : it.EmailAddress)),
Subject = email.Data.Subject
};
if (email.Data.CcAddresses.Any())
{
sendRequest.Cc = string.Join(",", email.Data.CcAddresses.Select(it => !string.IsNullOrEmpty(it.Name) ? $"{it.Name} <{it.EmailAddress}>" : it.EmailAddress));
}
if (email.Data.BccAddresses.Any())
{
sendRequest.Bcc = string.Join(",", email.Data.BccAddresses.Select(it => !string.IsNullOrEmpty(it.Name) ? $"{it.Name} <{it.EmailAddress}>" : it.EmailAddress));
}
if (email.Data.ReplyToAddresses.Any())
{
sendRequest.ReplyTo = string.Join(",", email.Data.ReplyToAddresses.Select(it => !string.IsNullOrEmpty(it.Name) ? $"{it.Name} <{it.EmailAddress}>" : it.EmailAddress));
}
if (email.Data.IsHtml)
{
sendRequest.HtmlBody = email.Data.Body;
if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
{
sendRequest.TextBody = email.Data.PlaintextAlternativeBody;
}
}
else
{
sendRequest.TextBody = email.Data.Body;
}
if (email.Data.Tags.Any())
{
sendRequest.Tags.AddRange(email.Data.Tags);
}
if (email.Data.Attachments.Any())
{
sendRequest.Attachments.AddRange(
email.Data.Attachments.Select(it => new MailPaceAttachment
{
Name = it.Filename,
Content = it.Data.ConvertToBase64(),
ContentType = it.ContentType ?? Path.GetExtension(it.Filename), // jpeg, jpg, png, gif, txt, pdf, docx, xlsx, pptx, csv, att, ics, ical, html, zip
Cid = it.IsInline ? it.ContentId : null
}));
}
return sendRequest;
}
private async Task<T> TryOrNull<T>(Func<Task<T>> method)
{
try
{
return await method();
}
catch
{
return default;
}
}
public void Dispose()
{
_httpClient.Dispose();
}
}