2026-07-27 05:33:53 +00:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Mail;
|
2026-08-12 11:04:04 +02:00
|
|
|
using Heimdall.Application.Common;
|
2026-07-26 20:45:35 +00:00
|
|
|
using Heimdall.Application.Services;
|
2026-08-12 11:04:04 +02:00
|
|
|
using Heimdall.Infrastructure.Settings;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-07-26 20:45:35 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Infrastructure.Services;
|
|
|
|
|
|
|
|
|
|
public class EmailService : IEmailService
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<EmailService> _logger;
|
2026-08-12 11:04:04 +02:00
|
|
|
private readonly IServiceProvider _services;
|
2026-07-26 20:45:35 +00:00
|
|
|
|
2026-10-26 06:26:00 +01:00
|
|
|
public EmailService(IServiceProvider services, ILogger<EmailService> logger)
|
2026-07-26 20:45:35 +00:00
|
|
|
{
|
2026-08-12 11:04:04 +02:00
|
|
|
_services = services;
|
2026-07-26 20:45:35 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SendEmailAsync(string recipient, string subject, string bodyHtml, CancellationToken ct = default)
|
|
|
|
|
{
|
2026-08-12 11:04:04 +02:00
|
|
|
var (smtpHost, smtpPort, username, password, fromAddress, fromName, useSsl) =
|
|
|
|
|
await ResolveConfigAsync(ct).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(smtpHost))
|
2026-07-27 05:33:53 +00:00
|
|
|
{
|
2026-10-24 22:27:53 +02:00
|
|
|
throw new InvalidOperationException("SMTP is not configured; email delivery cannot be completed.");
|
2026-07-27 05:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var message = new MailMessage
|
|
|
|
|
{
|
2026-08-12 11:04:04 +02:00
|
|
|
From = new MailAddress(fromAddress!, fromName),
|
2026-07-27 05:33:53 +00:00
|
|
|
Subject = subject,
|
|
|
|
|
Body = bodyHtml,
|
|
|
|
|
IsBodyHtml = true
|
|
|
|
|
};
|
|
|
|
|
message.To.Add(recipient);
|
|
|
|
|
|
2026-08-12 11:04:04 +02:00
|
|
|
using var client = new SmtpClient(smtpHost, smtpPort)
|
2026-07-27 05:33:53 +00:00
|
|
|
{
|
2026-08-12 11:04:04 +02:00
|
|
|
EnableSsl = useSsl,
|
2026-07-27 05:33:53 +00:00
|
|
|
DeliveryMethod = SmtpDeliveryMethod.Network
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-12 11:04:04 +02:00
|
|
|
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
|
2026-07-27 05:33:53 +00:00
|
|
|
{
|
2026-08-12 11:04:04 +02:00
|
|
|
client.Credentials = new NetworkCredential(username, password);
|
2026-07-27 05:33:53 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-27 18:59:12 +00:00
|
|
|
await client.SendMailAsync(message, ct).ConfigureAwait(false);
|
2026-07-27 05:33:53 +00:00
|
|
|
_logger.LogDebug("Email sent to {To}: {Subject}", recipient, subject);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2026-10-24 22:27:53 +02:00
|
|
|
throw;
|
2026-07-27 05:33:53 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to send email to {To}: {Subject}", recipient, subject);
|
|
|
|
|
throw; // Re-throw so Hangfire can retry
|
|
|
|
|
}
|
2026-07-26 20:45:35 +00:00
|
|
|
}
|
2026-08-12 11:04:04 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2026-10-26 06:26:00 +01:00
|
|
|
/// Source de vérité : AppSettings globaux en base (email.*), relus à
|
|
|
|
|
/// chaque envoi pour prendre en compte les modifications de l'IHM.
|
2026-08-12 11:04:04 +02:00
|
|
|
/// </summary>
|
|
|
|
|
private async Task<(string? SmtpHost, int SmtpPort, string? Username, string? Password,
|
|
|
|
|
string? FromAddress, string? FromName, bool UseSsl)> ResolveConfigAsync(CancellationToken ct)
|
|
|
|
|
{
|
|
|
|
|
using var scope = _services.CreateScope();
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
|
|
|
|
|
|
2026-10-26 06:26:00 +01:00
|
|
|
var smtpHost = await AppSettingReader.GetAsync(db, "email.smtpHost", ct).ConfigureAwait(false);
|
2026-08-12 11:04:04 +02:00
|
|
|
|
|
|
|
|
var smtpPort = await AppSettingReader.GetIntAsync(db, "email.smtpPort", -1, ct).ConfigureAwait(false);
|
|
|
|
|
if (smtpPort < 0)
|
2026-10-26 06:26:00 +01:00
|
|
|
smtpPort = 587;
|
2026-08-12 11:04:04 +02:00
|
|
|
|
2026-10-26 06:26:00 +01:00
|
|
|
var username = await AppSettingReader.GetAsync(db, "email.username", ct).ConfigureAwait(false);
|
2026-08-12 11:04:04 +02:00
|
|
|
|
2026-10-26 06:26:00 +01:00
|
|
|
var password = await AppSettingReader.GetAsync(db, "email.password", ct).ConfigureAwait(false);
|
2026-08-12 11:04:04 +02:00
|
|
|
|
|
|
|
|
var fromAddress = await AppSettingReader.GetAsync(db, "email.fromAddress", ct).ConfigureAwait(false)
|
2026-10-26 06:26:00 +01:00
|
|
|
?? "noreply@heimdall.local";
|
2026-08-12 11:04:04 +02:00
|
|
|
|
|
|
|
|
var fromName = await AppSettingReader.GetAsync(db, "email.fromName", ct).ConfigureAwait(false)
|
2026-10-26 06:26:00 +01:00
|
|
|
?? "Heimdall";
|
2026-08-12 11:04:04 +02:00
|
|
|
|
|
|
|
|
var useSsl = await AppSettingReader.GetBoolAsync(db, "email.useSsl", true, ct).ConfigureAwait(false);
|
|
|
|
|
return (smtpHost, smtpPort, username, password, fromAddress, fromName, useSsl);
|
|
|
|
|
}
|
2026-07-26 20:45:35 +00:00
|
|
|
}
|