-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailSmtp.cs
43 lines (33 loc) · 1.23 KB
/
EmailSmtp.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
using Healthcare.Application.Core.Abstractions.Email;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using MimeKit.Text;
namespace Healthcare.Infrastructure.Emails;
public sealed class EmailSmtp : IEmailSmtp
{
private readonly MailSettings _mailSettings;
public EmailSmtp(IOptions<MailSettings> mailSettings)
{
_mailSettings = mailSettings.Value;
}
public async Task SendEmailAsync(MailRequest mailRequest)
{
var email = new MimeMessage
{
Subject = mailRequest.Subject,
Body = new TextPart(TextFormat.Text)
{
Text = mailRequest.Body
}
};
email.From.Add(new MailboxAddress(_mailSettings.SenderDisplayName, _mailSettings.SenderEmail));
email.To.Add(MailboxAddress.Parse(mailRequest.EmailTo));
using var smtpClient = new SmtpClient();
await smtpClient.ConnectAsync(_mailSettings.SmtpServer, _mailSettings.SmtpPort, SecureSocketOptions.StartTls);
await smtpClient.AuthenticateAsync(_mailSettings.SenderEmail, _mailSettings.SmtpPassword);
await smtpClient.SendAsync(email);
await smtpClient.DisconnectAsync(true);
}
}