-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureCommunicationEmailSender.cs
More file actions
145 lines (127 loc) · 6.49 KB
/
AzureCommunicationEmailSender.cs
File metadata and controls
145 lines (127 loc) · 6.49 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
// <copyright file="AzureCommunicationEmailSender.cs" company="Moonrise Software, LLC">
// Copyright (c) Moonrise Software, LLC. All rights reserved.
// Licensed under the GNU Public License, Version 3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
// See https://github.com/MoonriseSoftwareCalifornia/CosmosCMS
// for more information concerning the license and the contributors participating to this project.
// </copyright>
namespace Cosmos.EmailServices
{
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Azure.Communication.Email;
using Azure.Core;
using Azure.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
/// <summary>
/// Email sender for Azure Communications.
/// </summary>
public class AzureCommunicationEmailSender : ICosmosEmailSender
{
private readonly IOptions<AzureCommunicationEmailProviderOptions> options;
private readonly ILogger<AzureCommunicationEmailSender> logger;
private readonly DefaultAzureCredential credential;
/// <summary>
/// Initializes a new instance of the <see cref="AzureCommunicationEmailSender"/> class.
/// </summary>
/// <param name="options">Provder options.</param>
/// <param name="logger">ILogger.</param>
/// <param name="defaultAzureCredential">Default Azure token credential.</param>
public AzureCommunicationEmailSender(IOptions<AzureCommunicationEmailProviderOptions> options, ILogger<AzureCommunicationEmailSender> logger, DefaultAzureCredential defaultAzureCredential)
{
this.options = options;
this.logger = logger;
this.credential = defaultAzureCredential;
this.SendResult = new SendResult();
}
/// <summary>
/// Gets result of the last email send.
/// </summary>
public SendResult SendResult { get; private set; }
/// <summary>
/// Sends an email message.
/// </summary>
/// <param name="toEmail">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="htmlMessage">Email content in HTML form.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SendEmailAsync(string toEmail, string subject, string htmlMessage)
{
await this.SendEmailAsync(toEmail, subject, htmlMessage, null);
}
/// <summary>
/// Sends an email and specifies the "from" email address.
/// </summary>
/// <param name="toEmail">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="htmlMessage">Email message in HTML.</param>
/// <param name="emailFrom">From email address.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SendEmailAsync(string toEmail, string subject, string htmlMessage, string? emailFrom)
{
await this.SendEmailAsync(toEmail, subject, string.Empty, htmlMessage, emailFrom);
}
/// <summary>
/// Sends an email and specifies the "from" email address.
/// </summary>
/// <param name="emailTo">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="textVersion">Email message in text form.</param>
/// <param name="htmlVersion">Email message in HTML.</param>
/// <param name="emailFrom">Who the email is from.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task SendEmailAsync(string emailTo, string subject, string textVersion, string htmlVersion, string? emailFrom = null)
{
var tempParts = this.options.Value.ConnectionString.Split(";").Where(w => !string.IsNullOrEmpty(w)).Select(part => part.Split('=')).ToDictionary(sp => sp[0], sp => sp[1], StringComparer.OrdinalIgnoreCase);
var tempEndPoint = tempParts["endpoint"];
EmailClient emailClient;
this.SendResult = new SendResult();
if (!tempParts.ContainsKey("AccessKey"))
{
this.SendResult.StatusCode = HttpStatusCode.InternalServerError;
this.SendResult.Message = "AccessKey not found in connection string.";
this.logger.LogInformation(this.SendResult.Message);
return;
}
if (tempParts["AccessKey"] == "AccessToken")
{
emailClient = new EmailClient(endpoint: new Uri(tempEndPoint), this.credential);
}
else
{
emailClient = new EmailClient(this.options.Value.ConnectionString);
}
if (string.IsNullOrEmpty(emailFrom))
{
emailFrom = this.options.Value.DefaultFromEmailAddress;
}
EmailSendOperation result;
var aschtml = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(htmlVersion));
if (string.IsNullOrEmpty(textVersion))
{
result = await emailClient.SendAsync(Azure.WaitUntil.Completed, emailFrom, emailTo, subject, htmlContent: aschtml);
}
else
{
var asctext = System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(textVersion));
result = await emailClient.SendAsync(Azure.WaitUntil.Completed, emailFrom, emailTo, subject, aschtml, asctext);
}
var response = result.GetRawResponse();
this.SendResult.StatusCode = (HttpStatusCode)response.Status;
if (result.Value.Status == EmailSendStatus.Succeeded)
{
this.SendResult.Message = $"Email successfully sent to: {emailTo}; Subject: {subject};";
}
else if (result.Value.Status == EmailSendStatus.Failed)
{
this.SendResult.Message = $"Email FAILED attempting to send to: {emailTo}, with subject: {subject}, with error: {response.ReasonPhrase}";
}
else if (result.Value.Status == EmailSendStatus.Canceled)
{
this.SendResult.Message = $"Email was CANCELED to: {emailTo}, with subject: {subject}, with reason: {response.ReasonPhrase}";
}
this.logger.LogInformation(this.SendResult.Message);
}
}
}