-
Notifications
You must be signed in to change notification settings - Fork 0
/
AwsSesEmailSender.cs
168 lines (150 loc) · 6.75 KB
/
AwsSesEmailSender.cs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Amazon;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using MimeKit;
using MimeKit.Utils;
using Nop.Core.Domain.Messages;
using Nop.Services.Media;
namespace Nop.Services.Messages
{
/// <summary>
/// Email sender
/// Referemce documentation http://www.mimekit.net/docs/html/Creating-Messages.htm
/// </summary>
public partial class AwsSesEmailSender : IEmailSender
{
private readonly IDownloadService _downloadService;
private readonly EmailAccountSettings _emailAccountSettings;
private readonly Logging.ILogger _logger;
/// <summary>
/// Ctor
/// </summary>
/// <param name="downloadService">Download service</param>
public AwsSesEmailSender(IDownloadService downloadService, EmailAccountSettings emailAccountSettings, Logging.ILogger logger)
{
this._downloadService = downloadService;
this._emailAccountSettings = emailAccountSettings;
this._logger = logger;
}
#region Sends an email (AWS SES API, attachment will be linked in body as html anchor(<a href='link'>link</a>) element)
/// <summary>
/// Sends an email
/// </summary>
/// <param name="emailAccount">Email account to use</param>
/// <param name="subject">Subject</param>
/// <param name="body">Body</param>
/// <param name="fromAddress">From address</param>
/// <param name="fromName">From display name</param>
/// <param name="toAddress">To address</param>
/// <param name="toName">To display name</param>
/// <param name="replyTo">ReplyTo address</param>
/// <param name="replyToName">ReplyTo display name</param>
/// <param name="bcc">BCC addresses list</param>
/// <param name="cc">CC addresses list</param>
/// <param name="attachmentFilePath">Attachment file path</param>
/// <param name="attachmentFileName">Attachment file name. If specified, then this file name will be sent to a recipient. Otherwise, "AttachmentFilePath" name will be used.</param>
/// <param name="attachedDownloadId">Attachment download ID (another attachedment)</param>
/// <param name="headers">Headers</param>
public virtual void SendEmail(EmailAccount emailAccount, string subject, string body,
string fromAddress, string fromName, string toAddress, string toName,
string replyTo = null, string replyToName = null,
IEnumerable<string> bcc = null, IEnumerable<string> cc = null,
string attachmentFilePath = null, string attachmentFileName = null,
int attachedDownloadId = 0, IDictionary<string, string> headers = null)
{
//attachment support (v2.0 with attachment)
var message = new MimeMessage();
message.From.Add(new MailboxAddress(fromName, fromAddress));
message.To.Add(new MailboxAddress(toName, toAddress));
//reply to
if (!string.IsNullOrEmpty(replyTo))
{
message.ReplyTo.Add(new MailboxAddress(replyTo));
}
//BCC
if (bcc != null)
{
foreach (var address in bcc.Where(bccValue => !string.IsNullOrWhiteSpace(bccValue)))
{
message.Bcc.Add(new MailboxAddress(address.Trim()));
}
}
//CC
if (cc != null)
{
foreach (var address in cc.Where(ccValue => !string.IsNullOrWhiteSpace(ccValue)))
{
message.Cc.Add(new MailboxAddress(address.Trim()));
}
}
if (headers != null)
foreach (var header in headers)
{
message.Headers.Add(new Header(header.Key, header.Value));
}
// Set the plain-text/html version of the message text
var builder = new BodyBuilder
{
HtmlBody = body,
};
//build message
message.Subject = subject;
//create the file attachment for this e-mail message
if (!string.IsNullOrEmpty(attachmentFilePath) &&
File.Exists(attachmentFilePath))
{
// create a linked resource for attachment
var resources = builder.LinkedResources.Add(attachmentFilePath);
resources.ContentId = MimeUtils.GenerateMessageId();
//add Attachments in body
//builder.HtmlBody += $"<a href=\"{attachmentFilePath}\">{attachmentFileName}</a>";
}
//another attachment?
if (attachedDownloadId > 0)
{
var download = _downloadService.GetDownloadById(attachedDownloadId);
if (download != null)
{
//we do not support URLs as attachments
if (!download.UseDownloadUrl)
{
var downloadFileName = !string.IsNullOrWhiteSpace(download.Filename) ? download.Filename : download.Id.ToString();
downloadFileName += download.Extension;
// create a linked resource for attachment
var resources = builder.LinkedResources.Add(downloadFileName,download.DownloadBinary,ContentType.Parse(download.ContentType));
resources.ContentId = MimeUtils.GenerateMessageId();
//add Attachments in body
//builder.HtmlBody += $"<a href=\"{downloadFileName}\">{downloadFileName}</a>";
}
}
}
//build message
message.Body = builder.ToMessageBody();
//send email
using (var client = new AmazonSimpleEmailServiceClient(_emailAccountSettings.AwsAccessId, _emailAccountSettings.AwsAccessKey, RegionEndpoint.USEast1))
{
try
{
var stream = new MemoryStream();
message.WriteTo(stream);
var response = client.SendRawEmail(new SendRawEmailRequest()
{
RawMessage = new RawMessage()
{
Data = stream
}
});
}
catch (Exception ex)
{
_logger.InsertLog(Core.Domain.Logging.LogLevel.Debug, "Error message: " + ex.Message);
}
}
}
#endregion
}
}