From e0e399f5edda8c6508d7edc1ddeecb653920c3b6 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 5 Aug 2026 11:03:59 +0200 Subject: [PATCH] Refactor LimilabsEmailService for async operations Modernized LimilabsEmailService by replacing synchronous SMTP operations with asynchronous counterparts (e.g., SendMessageAsync, CloseAsync). Introduced a new SmtpExtensions class with a CloseSafelyAsync method for safe disconnection. Removed the DisconnectSafely method and replaced its usage with the new extension method. Improved exception handling and removed redundant Task.CompletedTask calls. These changes enhance code readability, ensure safe resource cleanup, and align with asynchronous programming practices. --- .../Services/Extensions/SmtpExtensions.cs | 16 ++++++++++ .../Services/LimilabsEmailService.cs | 29 +++++-------------- 2 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Extensions/SmtpExtensions.cs diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Extensions/SmtpExtensions.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Extensions/SmtpExtensions.cs new file mode 100644 index 0000000..3459d98 --- /dev/null +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/Extensions/SmtpExtensions.cs @@ -0,0 +1,16 @@ +using Limilabs.Client.SMTP; + +namespace DigitalData.MessagingService.Infrastructure.Services.Extensions; + +public static class SmtpExtensions +{ + public static async Task CloseSafelyAsync(this Smtp smtp) + { + try + { + if (smtp.Connected) + await smtp.CloseAsync(); + } + catch { /* Ignore disconnect errors */ } + } +} \ No newline at end of file diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs index fd1685b..44e8c86 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsEmailService.cs @@ -7,6 +7,7 @@ using Limilabs.Mail; using Limilabs.Mail.Headers; using Microsoft.Extensions.Options; using DigitalData.MessagingService.Application.Common.Options; +using DigitalData.MessagingService.Infrastructure.Services.Extensions; namespace DigitalData.MessagingService.Infrastructure.Services; @@ -53,39 +54,37 @@ public class LimilabsEmailService( var mail = builder.Create(); - var result = smtp.SendMessage(mail); + var result = await smtp.SendMessageAsync(mail, cancellationToken); if (result.Status != SendMessageStatus.Success) { throw new InvalidOperationException($"Failed to send email. Status: {result.Status}"); } - smtp.Close(); + await smtp.CloseAsync(cancellationToken); await Task.CompletedTask; // For async consistency } catch (Limilabs.Client.ServerException ex) { - DisconnectSafely(smtp); + await smtp.CloseSafelyAsync(); throw new AuthenticationFailedException("SMTP authentication failed. Check credentials or OAuth2 configuration.", ex); } catch (Exception ex) { - DisconnectSafely(smtp); + await smtp.CloseSafelyAsync(); throw new InvalidOperationException("Failed to send email via SMTP server.", ex); } } - // --- Private Helper Methods --- - private async Task ConnectAndAuthenticateSmtpAsync(Smtp smtp, EmailAccountDto smtpAccount) { if (smtpAccount.SmtpUseSsl) { - smtp.ConnectSSL(smtpAccount.SmtpServer, smtpAccount.SmtpPort); + await smtp.ConnectSSLAsync(smtpAccount.SmtpServer, smtpAccount.SmtpPort); } else { - smtp.Connect(smtpAccount.SmtpServer, smtpAccount.SmtpPort); + await smtp.ConnectAsync(smtpAccount.SmtpServer, smtpAccount.SmtpPort); } if (smtpAccount.UseOAuth2) @@ -96,19 +95,7 @@ public class LimilabsEmailService( { var password = smtpAccount.PasswordEncrypted ? encryptionService.Decrypt(smtpAccount.Password) : smtpAccount.Password; - smtp.Login(smtpAccount.Username, password); + await smtp.LoginAsync(smtpAccount.Username, password); } - - await Task.CompletedTask; // For async consistency - } - - private static void DisconnectSafely(Smtp smtp) - { - try - { - if (smtp.Connected) - smtp.Close(); - } - catch { /* Ignore disconnect errors */ } } }