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 */ } } }