47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using MailKit.Net.Smtp;
|
|
using MailKit.Security;
|
|
using MimeKit;
|
|
using Microsoft.Extensions.Configuration;
|
|
using WorkFlowCheck.BL.Models;
|
|
using WorkFlowCheck.BL.Services.Interfaces;
|
|
|
|
namespace WorkFlowCheck.BL.Services
|
|
{
|
|
public class MessageService : IMessageService
|
|
{
|
|
private readonly EmailSettings _settings;
|
|
|
|
public MessageService(IConfiguration configuration)
|
|
{
|
|
_settings = configuration.GetSection("EmailSettings").Get<EmailSettings>()!;
|
|
}
|
|
|
|
public async Task<bool> SendMailAsync(List<string> recipients, string subject, string htmlBody)
|
|
{
|
|
var message = new MimeMessage();
|
|
message.From.Add(new MailboxAddress(_settings.SenderName, _settings.Username));
|
|
|
|
foreach (var recipient in recipients)
|
|
{
|
|
message.To.Add(MailboxAddress.Parse(recipient));
|
|
}
|
|
|
|
message.Subject = subject;
|
|
|
|
message.Body = new TextPart("html")
|
|
{
|
|
Text = htmlBody
|
|
};
|
|
|
|
using var smtp = new SmtpClient();
|
|
await smtp.ConnectAsync(_settings.SmtpServer, _settings.SmtpPort, SecureSocketOptions.StartTls);
|
|
await smtp.AuthenticateAsync(_settings.Username, _settings.Password);
|
|
await smtp.SendAsync(message);
|
|
await smtp.DisconnectAsync(true);
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|