-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMailer.cs
77 lines (67 loc) · 2.34 KB
/
Mailer.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
using System;
using System.Threading.Tasks;
using BtSqlBackupService.Entities;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Logging;
using MimeKit;
namespace BtSqlBackupService
{
/// <summary>
/// A simple electronic mail sender.
/// </summary>
public class Mailer
{
private readonly ILogger _logger;
private readonly SmtpClientSettings _smtpClientSettings;
public Mailer(SmtpClientSettings smtpClientSettings, ILogger logger)
{
_smtpClientSettings = smtpClientSettings;
_logger = logger;
}
/// <summary>
/// Sends an email message with an HTML body.
/// </summary>
/// <param name="toEmailAddress">The email address of the receiver</param>
/// <param name="subject">The email's subject</param>
/// <param name="body">The HTML or text body of the emial</param>
/// <returns>A task for the completion of the command.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown when the underlying implementation throws any errors.
/// </exception>
public async Task SendEmailAsync(string toEmailAddress, string subject,
string body)
{
try
{
_logger.LogInformation(
$"Sending email to {toEmailAddress}, subject '{subject}'.");
var message = new MimeMessage();
message.Sender = new MailboxAddress(_smtpClientSettings.SenderName,
_smtpClientSettings.SenderEmail);
foreach (var address in toEmailAddress.Split(';'))
{
message.To.Add(MailboxAddress.Parse(address));
}
message.Subject = subject;
message.Body = new TextPart("html") {Text = body};
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
await client.ConnectAsync(_smtpClientSettings.Server,
_smtpClientSettings.Port);
await client.AuthenticateAsync(_smtpClientSettings.Username,
_smtpClientSettings.Password);
await client.SendAsync(message);
await client.DisconnectAsync(true);
_logger.LogInformation(
$"Email sent to {toEmailAddress}, subject '{subject}'.");
}
}
catch (Exception e)
{
_logger.LogError(
$"Failed to send email to {toEmailAddress}: {e.Message}");
}
}
}
}