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.
This commit is contained in:
@@ -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 */ }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user