From 42b30d4ac42c984fd1e9d96ae9831e5eebcfd20c Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 5 Aug 2026 14:14:27 +0200 Subject: [PATCH] Refactor SendEmailAsync to use EmailContext object Simplified the `SendEmailAsync` method in the `IEmailService` interface to accept a single `EmailContext` object instead of multiple parameters. Updated the `SendingEmailConsumer` and `LimilabsEmailService` classes to align with this change. In `LimilabsEmailService`, refactored email construction logic to use properties from the `EmailContext` object, including `Sender`, `Recipients`, `Subject`, `Body`, and `IsHtml`. Updated `ConnectAndAuthenticateSmtpAsync` to use the `Sender` property from `EmailContext`. These changes improve code readability, reduce parameter complexity, and ensure consistency across the email service implementation. --- .../Common/Interfaces/IEmailService.cs | 2 +- .../Queue/SendingEmailConsumer.cs | 7 +------ .../Services/LimilabsEmailService.cs | 20 ++++++++----------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IEmailService.cs b/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IEmailService.cs index 7a663ea..bb5dcf8 100644 --- a/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IEmailService.cs +++ b/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IEmailService.cs @@ -14,5 +14,5 @@ public interface IEmailService /// Sends an email using the configured SMTP account. /// SMTP credentials are configured in appsettings.json (EmailAccount section). /// - Task SendEmailAsync(EmailAccountDto from, IEnumerable recipients, string subject, string body, bool isHtml = true, CancellationToken cancellationToken = default); + Task SendEmailAsync(EmailContext context, CancellationToken cancellationToken = default); } diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Queue/SendingEmailConsumer.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Queue/SendingEmailConsumer.cs index 596835d..4101c4f 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Queue/SendingEmailConsumer.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Queue/SendingEmailConsumer.cs @@ -47,12 +47,7 @@ public sealed class SendingEmailConsumer : IAsyncDisposable if (oMailEvent is not null) { // Send email via SMTP (SMTP config is injected in IEmailService via IOptions) - await EmailService.SendEmailAsync( - oMailEvent.Mail.Sender, - oMailEvent.Mail.Recipients, - oMailEvent.Mail.Subject, - oMailEvent.Mail.Body, - isHtml: oMailEvent.Mail.IsHtml); + await EmailService.SendEmailAsync(oMailEvent.Mail); // Acknowledge message after successful processing await channel.BasicAckAsync(args.DeliveryTag, false, args.CancellationToken); diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs index f65f793..dae58e1 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs @@ -25,30 +25,26 @@ public class LimilabsEmailService( Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); } - public async Task SendEmailAsync(EmailAccountDto from, IEnumerable recipients, string subject, string body, bool isHtml = true, CancellationToken cancellationToken = default) + public async Task SendEmailAsync(EmailContext context, CancellationToken cancellationToken = default) { using var smtp = new Smtp(); try { - await ConnectAndAuthenticateSmtpAsync(smtp, from); + await ConnectAndAuthenticateSmtpAsync(smtp, context.Sender); var builder = new MailBuilder(); - builder.From.Add(new MailBox(from.Username)); + builder.From.Add(new MailBox(context.Sender.Username)); - foreach (var recipient in recipients) + foreach (var recipient in context.Recipients) builder.To.Add(new MailBox(recipient)); - builder.Subject = subject; + builder.Subject = context.Subject; - if (isHtml) - { - builder.Html = body; - } + if (context.IsHtml) + builder.Html = context.Body; else - { - builder.Text = body; - } + builder.Text = context.Body; var mail = builder.Create();