MailKit-tel

email küldése
This commit is contained in:
2025-04-10 19:04:59 +02:00
parent 5356ef7424
commit 623a3c49b1
8 changed files with 124 additions and 6 deletions
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkFlowCheck.BL.Services.Interfaces
{
public interface IMessageService
{
Task<bool> SendMailAsync(List<string> recipients, string subject, string htmlBody);
}
}
@@ -0,0 +1,46 @@
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;
}
}
}