MailKit-tel
email küldése
This commit is contained in:
@@ -17,6 +17,7 @@ namespace WorkFlowCheck.BL.Infra
|
||||
service.AddScoped<ISyncService, SyncService>();
|
||||
service.AddScoped<ICheckListService, CheckListService>();
|
||||
service.AddScoped<INumberGeneratorService, NumberGeneratorService>();
|
||||
service.AddScoped<IMessageService, MessageService>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.BL.Models
|
||||
{
|
||||
public class EmailSettings
|
||||
{
|
||||
public string SmtpServer { get; set; } = string.Empty;
|
||||
public int SmtpPort { get; set; }
|
||||
public string Username { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public string SenderName { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="DocumentFormat.OpenXml" Version="3.3.0" />
|
||||
<PackageReference Include="MailKit" Version="4.11.0" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
|
||||
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
|
||||
Reference in New Issue
Block a user