Refactor IMAP initialization with OpenAsync helper
Refactored IMAP object initialization and folder selection into a new `OpenAsync` helper method to reduce code duplication and improve maintainability. - Replaced repetitive connection, authentication, and folder selection logic in multiple methods (`FetchEmailsAsync`, `FetchEmailUidsAsync`, etc.) with `OpenAsync`. - Removed `ConnectAndAuthenticateAsync` and `SelectFolderAsync` methods, as their functionality is now encapsulated in `OpenAsync`. - Updated `FetchEmailUidsAsync` to remove the `EmailAccountDto account` parameter, delegating connection logic to `OpenAsync`. - Ensured all methods now use `OpenAsync` to obtain a fully prepared `Imap` instance, improving clarity and reducing error risk.
This commit is contained in:
@@ -33,10 +33,10 @@ public class LimilabsImapEmailService(
|
||||
MailSearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
using var imap = await OpenAsync(account, filter.Folder);
|
||||
try
|
||||
{
|
||||
var uids = await FetchEmailUidsAsync(imap, account, filter, cancellationToken);
|
||||
var uids = await FetchEmailUidsAsync(imap, filter, cancellationToken);
|
||||
|
||||
if (uids.Count == 0)
|
||||
return [];
|
||||
@@ -84,13 +84,10 @@ public class LimilabsImapEmailService(
|
||||
MailSearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
using var imap = await OpenAsync(account, filter.Folder);
|
||||
try
|
||||
{
|
||||
await ConnectAndAuthenticateAsync(imap, account);
|
||||
await SelectFolderAsync(imap, filter.Folder);
|
||||
|
||||
List<long> uids = await FetchEmailUidsAsync(imap, account, filter, cancellationToken);
|
||||
List<long> uids = await FetchEmailUidsAsync(imap, filter, cancellationToken);
|
||||
|
||||
await imap.CloseAsync(cancellationToken);
|
||||
return uids;
|
||||
@@ -116,12 +113,9 @@ public class LimilabsImapEmailService(
|
||||
bool withAttachments = false,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
using var imap = await OpenAsync(account, folder);
|
||||
try
|
||||
{
|
||||
await ConnectAndAuthenticateAsync(imap, account);
|
||||
await SelectFolderAsync(imap, folder);
|
||||
|
||||
var mail = await FetchEmailByUidAsync(imap, uid, withAttachments, cancellationToken);
|
||||
|
||||
await imap.CloseAsync(cancellationToken);
|
||||
@@ -147,11 +141,9 @@ public class LimilabsImapEmailService(
|
||||
string folder = "INBOX",
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
using var imap = await OpenAsync(account, folder);
|
||||
try
|
||||
{
|
||||
await ConnectAndAuthenticateAsync(imap, account);
|
||||
await SelectFolderAsync(imap, folder);
|
||||
await imap.MarkMessageSeenByUIDAsync(uid, cancellationToken);
|
||||
await imap.CloseAsync(cancellationToken);
|
||||
}
|
||||
@@ -172,13 +164,9 @@ public class LimilabsImapEmailService(
|
||||
// Private helpers
|
||||
private async Task<List<long>> FetchEmailUidsAsync(
|
||||
Imap imap,
|
||||
EmailAccountDto account,
|
||||
MailSearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
await ConnectAndAuthenticateAsync(imap, account);
|
||||
await SelectFolderAsync(imap, filter.Folder);
|
||||
|
||||
List<ICriterion> criterions = [];
|
||||
|
||||
if (filter.UnseenOnly)
|
||||
@@ -286,8 +274,10 @@ public class LimilabsImapEmailService(
|
||||
return withAttachments || email is null ? email : email with { Attachments = [] };
|
||||
}
|
||||
|
||||
private async Task ConnectAndAuthenticateAsync(Imap imap, EmailAccountDto account)
|
||||
private async Task<Imap> OpenAsync(EmailAccountDto account, string folder)
|
||||
{
|
||||
var imap = new Imap();
|
||||
|
||||
if (account.ImapUseSsl)
|
||||
await imap.ConnectSSLAsync(account.ImapServer!, account.ImapPort);
|
||||
else
|
||||
@@ -298,13 +288,12 @@ public class LimilabsImapEmailService(
|
||||
: account.Password;
|
||||
|
||||
await imap.LoginAsync(account.Username, password);
|
||||
}
|
||||
|
||||
private static async Task SelectFolderAsync(Imap imap, string folder)
|
||||
{
|
||||
if (string.Equals(folder, "INBOX", StringComparison.OrdinalIgnoreCase))
|
||||
await imap.SelectInboxAsync();
|
||||
else
|
||||
await imap.SelectAsync(folder);
|
||||
|
||||
return imap;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user