Refactor email account handling for dynamic resolution

Reintroduced `EmailAccountDto` with conditional compilation to support both .NET and non-.NET environments. Updated `IEmailService` to accept `EmailAccountDto` as the sender, replacing reliance on pre-configured SMTP credentials.

Added `GetSenderQuery` and its handler to dynamically resolve email accounts based on `Id` or `Username`. Introduced `GetSenderQueryValidator` for validation, ensuring proper usage of the query.

Modified `SendEmailCommand` to include sender resolution via MediatR. Updated `OutgoingEmailEvent` to include sender information and adjusted `OutgoingEmailConsumer` and `LimilabsEmailService` to use the dynamically resolved sender.

Updated `EmailMappingProfile` to ignore the `Sender` property during mapping. Replaced `Name` with `Id` in `appsettings.Secrets.json` for email accounts. Removed the old `EmailAccountDto` folder and performed general cleanup and restructuring.
This commit is contained in:
2026-08-05 12:32:08 +02:00
parent 7fa3c4888a
commit 740bb8c313
12 changed files with 136 additions and 45 deletions

View File

@@ -19,8 +19,7 @@ namespace DigitalData.MessagingService.Infrastructure.Services;
/// or falls back to the first account if none is named "default".
/// </summary>
public class LimilabsEmailService(
IEncryptionService encryptionService,
IOptions<EmailAccountsOptions> smtpConfig) : IEmailService
IEncryptionService encryptionService) : IEmailService
{
// Register encoding provider for Limilabs (requires windows-1252 and other code pages)
static LimilabsEmailService()
@@ -28,18 +27,16 @@ public class LimilabsEmailService(
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
public async Task SendEmailAsync(string to, string subject, string body, bool isHtml = true, CancellationToken cancellationToken = default)
public async Task SendEmailAsync(EmailAccountDto from, string to, string subject, string body, bool isHtml = true, CancellationToken cancellationToken = default)
{
var smtpAccount = smtpConfig.Value.Accounts.First();
using var smtp = new Smtp();
try
{
await ConnectAndAuthenticateSmtpAsync(smtp, smtpAccount);
await ConnectAndAuthenticateSmtpAsync(smtp, from);
var builder = new MailBuilder();
builder.From.Add(new MailBox(smtpAccount.Username));
builder.From.Add(new MailBox(from.Username));
builder.To.Add(new MailBox(to));
builder.Subject = subject;