-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathSESSender.cs
More file actions
155 lines (131 loc) · 5.33 KB
/
Copy pathSESSender.cs
File metadata and controls
155 lines (131 loc) · 5.33 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
144
145
146
147
148
149
150
151
152
153
154
155
using Amazon;
using Amazon.SimpleEmailV2;
using Amazon.SimpleEmailV2.Model;
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using Microsoft.Extensions.Options;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Attachment = FluentEmail.Core.Models.Attachment;
namespace FluentEmail.SES
{
public class SESSender : ISender
{
private readonly AmazonSimpleEmailServiceV2Client _emailClient;
public SESSender(IOptions<FluenEmailSESOptions> options)
{
var config = new AmazonSimpleEmailServiceV2Config()
{
RegionEndpoint = RegionEndpoint.GetBySystemName(options.Value.RegionEndpoint)
};
_emailClient = new AmazonSimpleEmailServiceV2Client(
options.Value.AccessKeyId,
options.Value.SecretAccessKey,
config);
}
public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
{
return SendAsync(email, token).GetAwaiter().GetResult();
}
public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token = null)
{
var response = new SendResponse();
if (token?.IsCancellationRequested ?? false)
{
response.ErrorMessages.Add("Message was cancelled by cancellation token.");
return response;
}
try
{
var message = new MimeMessage();
if (string.IsNullOrWhiteSpace(email.Data.Subject))
{
response.ErrorMessages.Add("Subject is missing.");
}
if (email.Data.ToAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.To.AddRange(email.Data.ToAddresses.Select(ConvertAddress));
}
if (email.Data.CcAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.Cc.AddRange(email.Data.CcAddresses.Select(ConvertAddress));
}
if (email.Data.BccAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.Bcc.AddRange(email.Data.BccAddresses.Select(ConvertAddress));
}
if (email.Data.ReplyToAddresses.Any(a => !string.IsNullOrWhiteSpace(a.EmailAddress)))
{
message.ReplyTo.AddRange(email.Data.ReplyToAddresses.Select(ConvertAddress));
}
switch (email.Data.Priority)
{
case Priority.High:
message.Priority = MessagePriority.Urgent;
break;
case Priority.Normal:
// Do not set anything.
// Leave default values. It means Normal Priority.
break;
case Priority.Low:
message.Priority = MessagePriority.NonUrgent;
break;
}
message.From.Add(ConvertAddress(email.Data.FromAddress));
message.Subject = email.Data.Subject;
var bodyBuilder = new BodyBuilder();
if (!string.IsNullOrEmpty(email.Data.PlaintextAlternativeBody))
{
bodyBuilder.TextBody = email.Data.PlaintextAlternativeBody;
bodyBuilder.HtmlBody = email.Data.Body;
}
else if (!email.Data.IsHtml)
{
bodyBuilder.TextBody = email.Data.Body;
}
else
{
bodyBuilder.HtmlBody = email.Data.Body;
}
foreach (var attachment in email.Data.Attachments ?? new List<Attachment>())
{
if (attachment.Data != null)
{
bodyBuilder.Attachments.Add(
attachment.Filename,
attachment.Data,
MimeKit.ContentType.Parse(attachment.ContentType ?? "application/octet-stream")
);
}
}
message.Body = bodyBuilder.ToMessageBody();
using var messageStream = new MemoryStream();
await message.WriteToAsync(messageStream);
messageStream.Seek(0, SeekOrigin.Begin);
var emailResponse = await _emailClient.SendEmailAsync(new SendEmailRequest
{
Content = new EmailContent
{
Raw = new RawMessage()
{
Data = messageStream
}
}
});
response.MessageId = emailResponse.MessageId;
}
catch (Exception ex)
{
response.ErrorMessages.Add(ex.Message);
}
return response;
}
private MailboxAddress ConvertAddress(Address address) => new MailboxAddress(address.Name, address.EmailAddress);
}
}