-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmtpEmailSender.cs
More file actions
153 lines (133 loc) · 5.68 KB
/
SmtpEmailSender.cs
File metadata and controls
153 lines (133 loc) · 5.68 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
// <copyright file="SmtpEmailSender.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.Net;
using System.Net.Mail;
using Microsoft.Extensions.Options;
/// <summary>
/// SMTP Email sender.
/// </summary>
public class SmtpEmailSender : ICosmosEmailSender
{
private readonly SmtpEmailProviderOptions options;
/// <summary>
/// Initializes a new instance of the <see cref="SmtpEmailSender"/> class.
/// </summary>
/// <param name="options">SMTP email provider options.</param>
public SmtpEmailSender(IOptions<SmtpEmailProviderOptions> options)
{
this.options = options.Value;
if (options.Value == null)
{
throw new InvalidOperationException("No SmtpEmailProviderOptions configuration found.");
}
this.SendResult = new SendResult();
}
/// <summary>
/// Gets the status code of the last email send result.
/// </summary>
public SendResult SendResult { get; private set; }
/// <summary>
/// IEmailSender send email method.
/// </summary>
/// <param name="emailTo">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="htmlMessage">Email message in HTML format.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task SendEmailAsync(string emailTo, string subject, string htmlMessage)
{
return this.SendEmailAsync(emailTo, subject, htmlMessage, null);
}
/// <summary>
/// Send email method.
/// </summary>
/// <param name="emailTo">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="htmlMessage">Email message in HTML format.</param>
/// <param name="emailFrom">From email message.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task SendEmailAsync(string emailTo, string subject, string htmlMessage, string? emailFrom = null)
{
var message = new MailMessage();
if (string.IsNullOrEmpty(emailFrom))
{
emailFrom = this.options.DefaultFromEmailAddress;
}
message.Subject = subject;
#pragma warning disable CS8604 // Possible null reference argument.
message.From = new MailAddress(emailFrom);
#pragma warning restore CS8604 // Possible null reference argument.
message.To.Add(new MailAddress(emailTo));
message.Body = htmlMessage;
message.IsBodyHtml = true;
var task = this.Execute(message);
task.Wait();
return task;
}
/// <summary>
/// Sends and Email using a SendGrid mail template.
/// </summary>
/// <param name="emailTo">To email address.</param>
/// <param name="subject">Email subject.</param>
/// <param name="textVersion">Text version of the email.</param>
/// <param name="htmlVersion">HTML version of the email.</param>
/// <param name="emailFrom">Who the email is from.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task SendEmailAsync(string emailTo, string subject, string textVersion, string htmlVersion, string? emailFrom = null)
{
var message = new MailMessage();
if (string.IsNullOrEmpty(emailFrom))
{
emailFrom = this.options.DefaultFromEmailAddress;
}
message.Subject = subject;
#pragma warning disable CS8604 // Possible null reference argument.
message.From = new MailAddress(emailFrom);
#pragma warning restore CS8604 // Possible null reference argument.
message.To.Add(new MailAddress(emailTo));
message.Body = textVersion;
if (!string.IsNullOrEmpty(htmlVersion))
{
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlVersion, null, "text/html"));
message.IsBodyHtml = true;
}
else
{
message.IsBodyHtml = false;
}
message.Subject = subject;
var task = this.Execute(message);
task.Wait();
return task;
}
private async Task Execute(MailMessage message)
{
var client = new SmtpClient(this.options.Host, this.options.Port);
if (!string.IsNullOrEmpty(this.options.Password))
{
client.Credentials = new NetworkCredential(this.options.UserName, this.options.Password);
if (this.options.UsesSsl)
{
client.EnableSsl = true;
}
}
try
{
await client.SendMailAsync(message);
this.SendResult.StatusCode = HttpStatusCode.OK;
this.SendResult.Message = "Email sent successfully.";
}
catch (Exception ex)
{
this.SendResult.StatusCode = HttpStatusCode.BadRequest;
this.SendResult.Message = ex.Message;
}
return;
}
}
}