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
@@ -15,10 +15,16 @@ namespace WorkFlowCheck.API.Controllers
{
private ISyncService _syncService;
private INumberGeneratorService _numberGeneratorService;
public SyncController(ISyncService syncService,INumberGeneratorService numberGeneratorService)
private IMessageService _messageService;
public SyncController(ISyncService syncService,
INumberGeneratorService numberGeneratorService,
IMessageService messageService)
{
_syncService = syncService;
_numberGeneratorService = numberGeneratorService;
_messageService = messageService;
}
@@ -490,5 +496,31 @@ namespace WorkFlowCheck.API.Controllers
return StatusCode(500, $"Error: {ex.Message}");
}
}
[HttpGet("SendTestMail")]
public async Task<ApiResponseDTO<bool>> SendTestMail()
{
var retVal = new ApiResponseDTO<bool>()
{
IsSuccess = true
};
try
{
var result = await _messageService.SendMailAsync(
new List<string>()
{
"ivan.szabo.sis@gmail.com",
}, "Teszt", "Ok");
retVal.Data = true;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
retVal.Errors.Add(ex.Message);
Log.Error(ex.Message);
}
return retVal;
}
}
}
@@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="4.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.12">
<PrivateAssets>all</PrivateAssets>
+7
View File
@@ -37,5 +37,12 @@
"Url": "http://localhost:8660"
}
}
},
"EmailSettings": {
"SmtpServer": "smtp.gmail.com",
"SmtpPort": 587,
"Username": "nuvolarsetup@gmail.com",
"Password": "reiq pgfr nxub pvrb",
"SenderName": "Nuvolar System"
}
}
+1
View File
@@ -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" />