Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 5f36623

Browse files
committed
Aggiunta implementazione MailKit
1 parent 7c02cfb commit 5f36623

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NET6CustomLibrary.MailKit.Options;
2+
3+
public class SmtpOptions
4+
{
5+
public string Host { get; set; }
6+
public int Port { get; set; }
7+
public SecureSocketOptions Security { get; set; }
8+
public string Username { get; set; }
9+
public string Password { get; set; }
10+
public string Sender { get; set; }
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NET6CustomLibrary.MailKit.Services;
2+
3+
public interface IEmailClient : IEmailSender
4+
{
5+
Task<bool> SendEmailAsync(string recipientEmail, string replyToEmail, string subject, string htmlMessage, CancellationToken token = default);
6+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
namespace NET6CustomLibrary.MailKit.Services;
2+
3+
public class MailKitEmailSender : IEmailClient
4+
{
5+
private readonly ILoggerService loggerService;
6+
private readonly IOptionsMonitor<SmtpOptions> smtpOptionsMonitor;
7+
8+
public MailKitEmailSender(ILoggerService loggerService, IOptionsMonitor<SmtpOptions> smtpOptionsMonitor)
9+
{
10+
this.loggerService = loggerService;
11+
this.smtpOptionsMonitor = smtpOptionsMonitor;
12+
}
13+
14+
public Task SendEmailAsync(string email, string subject, string htmlMessage)
15+
{
16+
return SendEmailAsync(email, string.Empty, subject, htmlMessage);
17+
}
18+
19+
public async Task<bool> SendEmailAsync(string recipientEmail, string replyToEmail, string subject,
20+
string htmlMessage, CancellationToken token = default)
21+
{
22+
try
23+
{
24+
var options = smtpOptionsMonitor.CurrentValue;
25+
26+
using SmtpClient client = new();
27+
28+
await client.ConnectAsync(options.Host, options.Port, options.Security, token);
29+
30+
if (!string.IsNullOrEmpty(options.Username))
31+
{
32+
await client.AuthenticateAsync(options.Username, options.Password, token);
33+
}
34+
35+
MimeMessage message = new();
36+
37+
message.From.Add(MailboxAddress.Parse(options.Sender));
38+
message.To.Add(MailboxAddress.Parse(recipientEmail));
39+
40+
if (replyToEmail is not (null or ""))
41+
{
42+
message.ReplyTo.Add(MailboxAddress.Parse(replyToEmail));
43+
}
44+
45+
message.Subject = subject;
46+
message.Body = new TextPart("html")
47+
{
48+
Text = htmlMessage
49+
};
50+
51+
await client.SendAsync(message, token);
52+
await client.DisconnectAsync(true, token);
53+
54+
loggerService.SaveLogInformation($"Message successfully sent to the email address {recipientEmail}");
55+
return true;
56+
}
57+
catch
58+
{
59+
loggerService.SaveLogError($"Couldn't send email to {recipientEmail} with message {htmlMessage}");
60+
return false;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)