Add SMTP email delivery and deployment config

This commit is contained in:
GonnaHetzMe committed 2026-11-19 13:41:42 +01:00
1 parent 28fc49ea39
commit c96eb155ae
12 files changed
+160 -28

No files matched your search

@@ -1,27 +1,19 @@
using Helpdesk.Application.Abstractions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Helpdesk.Infrastructure.Email;
/// <summary>
/// Vývojová náhrada odesílání e-mailů – obsah zapíše do logu (odkaz pro ověření e-mailu je tak vidět
/// v <c>docker compose logs api</c>). Odesílání přes SMTP (MailKit + Mailpit) přijde s notifikacemi.
/// Vývojová náhrada odesílání e-mailů, pokud schránka SMTP není nakonfigurována.
/// </summary>
internal sealed partial class LoggingEmailSender(ILogger<LoggingEmailSender> logger, IHostEnvironment environment) : IEmailSender
internal sealed partial class LoggingEmailSender(ILogger<LoggingEmailSender> logger) : IEmailSender
{
public Task SendAsync(string to, string subject, string body, CancellationToken cancellationToken = default)
{
if (environment.IsDevelopment())
LogEmail(to, subject, body);
else
LogMissingTransport();
LogEmail(to, subject, body);
return Task.CompletedTask;
}
[LoggerMessage(Level = LogLevel.Information, Message = "E-mail pro {To} – {Subject}:\n{Body}")]
private partial void LogEmail(string to, string subject, string body);
[LoggerMessage(Level = LogLevel.Warning, Message = "E-mail nebyl odeslán. Pro toto prostředí není nakonfigurován poštovní transport.")]
private partial void LogMissingTransport();
}
@@ -0,0 +1,57 @@
using System.Net.Sockets;
using Helpdesk.Application.Abstractions;
using Helpdesk.Application.Common;
using MailKit;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MimeKit;
namespace Helpdesk.Infrastructure.Email;
internal sealed class SmtpOptions
{
public string Host { get; set; } = "smtp.seznam.cz";
public int Port { get; set; } = 465;
public string Mailbox { get; set; } = "";
public string Password { get; set; } = "";
}
internal sealed partial class SmtpEmailSender(IOptions<SmtpOptions> options, ILogger<SmtpEmailSender> logger) : IEmailSender
{
public async Task SendAsync(string to, string subject, string body, CancellationToken cancellationToken = default)
{
if (!MailboxAddress.TryParse(to, out var recipient))
{
LogDeliveryFailure("InvalidRecipient");
throw new EmailDeliveryException();
}
var settings = options.Value;
using var message = new MimeMessage { Subject = subject, Body = new TextPart("plain") { Text = body } };
message.From.Add(MailboxAddress.Parse(settings.Mailbox));
message.To.Add(recipient);
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(TimeSpan.FromSeconds(15));
using var client = new SmtpClient();
try
{
await client.ConnectAsync(settings.Host, settings.Port, SecureSocketOptions.SslOnConnect, timeout.Token);
await client.AuthenticateAsync(settings.Mailbox, settings.Password, timeout.Token);
await client.SendAsync(message, timeout.Token);
}
catch (Exception exception) when (!cancellationToken.IsCancellationRequested &&
exception is (IOException or SocketException or CommandException or ProtocolException or
AuthenticationException or SslHandshakeException or NotSupportedException or TimeoutException or
OperationCanceledException))
{
LogDeliveryFailure(exception.GetType().Name);
throw new EmailDeliveryException();
}
}
[LoggerMessage(Level = LogLevel.Warning, Message = "Doručení e-mailu přes SMTP selhalo ({FailureType}).")]
private partial void LogDeliveryFailure(string failureType);
}