Files
Heimdall/src/Heimdall.Infrastructure/Services/EmailService.cs
T
Arnaud Dartois d774d28f2a
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and scan server image (pull_request) Canceled after 0s
Heimdall Gitea / Build and scan agent image (pull_request) Canceled after 0s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Canceled after 0s
Heimdall Gitea / Publish versioned release (pull_request) Canceled after 0s
Heimdall Gitea / Build and tests (pull_request) Canceled after 7m33s
Heimdall Gitea / CI policy (pull_request) Failing after 2s
refactor(settings): manage integrations through Central UI
2026-10-26 06:26:00 +01:00

98 lines
3.6 KiB
C#

using System.Net;
using System.Net.Mail;
using Heimdall.Application.Common;
using Heimdall.Application.Services;
using Heimdall.Infrastructure.Settings;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Heimdall.Infrastructure.Services;
public class EmailService : IEmailService
{
private readonly ILogger<EmailService> _logger;
private readonly IServiceProvider _services;
public EmailService(IServiceProvider services, ILogger<EmailService> logger)
{
_services = services;
_logger = logger;
}
public async Task SendEmailAsync(string recipient, string subject, string bodyHtml, CancellationToken ct = default)
{
var (smtpHost, smtpPort, username, password, fromAddress, fromName, useSsl) =
await ResolveConfigAsync(ct).ConfigureAwait(false);
if (string.IsNullOrEmpty(smtpHost))
{
throw new InvalidOperationException("SMTP is not configured; email delivery cannot be completed.");
}
try
{
using var message = new MailMessage
{
From = new MailAddress(fromAddress!, fromName),
Subject = subject,
Body = bodyHtml,
IsBodyHtml = true
};
message.To.Add(recipient);
using var client = new SmtpClient(smtpHost, smtpPort)
{
EnableSsl = useSsl,
DeliveryMethod = SmtpDeliveryMethod.Network
};
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
client.Credentials = new NetworkCredential(username, password);
}
await client.SendMailAsync(message, ct).ConfigureAwait(false);
_logger.LogDebug("Email sent to {To}: {Subject}", recipient, subject);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send email to {To}: {Subject}", recipient, subject);
throw; // Re-throw so Hangfire can retry
}
}
/// <summary>
/// Source de vérité : AppSettings globaux en base (email.*), relus à
/// chaque envoi pour prendre en compte les modifications de l'IHM.
/// </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>();
var smtpHost = await AppSettingReader.GetAsync(db, "email.smtpHost", ct).ConfigureAwait(false);
var smtpPort = await AppSettingReader.GetIntAsync(db, "email.smtpPort", -1, ct).ConfigureAwait(false);
if (smtpPort < 0)
smtpPort = 587;
var username = await AppSettingReader.GetAsync(db, "email.username", ct).ConfigureAwait(false);
var password = await AppSettingReader.GetAsync(db, "email.password", ct).ConfigureAwait(false);
var fromAddress = await AppSettingReader.GetAsync(db, "email.fromAddress", ct).ConfigureAwait(false)
?? "noreply@heimdall.local";
var fromName = await AppSettingReader.GetAsync(db, "email.fromName", ct).ConfigureAwait(false)
?? "Heimdall";
var useSsl = await AppSettingReader.GetBoolAsync(db, "email.useSsl", true, ct).ConfigureAwait(false);
return (smtpHost, smtpPort, username, password, fromAddress, fromName, useSsl);
}
}