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,
|
MailSearchFilter filter,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var imap = new Imap();
|
using var imap = await OpenAsync(account, filter.Folder);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var uids = await FetchEmailUidsAsync(imap, account, filter, cancellationToken);
|
var uids = await FetchEmailUidsAsync(imap, filter, cancellationToken);
|
||||||
|
|
||||||
if (uids.Count == 0)
|
if (uids.Count == 0)
|
||||||
return [];
|
return [];
|
||||||
@@ -84,13 +84,10 @@ public class LimilabsImapEmailService(
|
|||||||
MailSearchFilter filter,
|
MailSearchFilter filter,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var imap = new Imap();
|
using var imap = await OpenAsync(account, filter.Folder);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ConnectAndAuthenticateAsync(imap, account);
|
List<long> uids = await FetchEmailUidsAsync(imap, filter, cancellationToken);
|
||||||
await SelectFolderAsync(imap, filter.Folder);
|
|
||||||
|
|
||||||
List<long> uids = await FetchEmailUidsAsync(imap, account, filter, cancellationToken);
|
|
||||||
|
|
||||||
await imap.CloseAsync(cancellationToken);
|
await imap.CloseAsync(cancellationToken);
|
||||||
return uids;
|
return uids;
|
||||||
@@ -116,12 +113,9 @@ public class LimilabsImapEmailService(
|
|||||||
bool withAttachments = false,
|
bool withAttachments = false,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var imap = new Imap();
|
using var imap = await OpenAsync(account, folder);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ConnectAndAuthenticateAsync(imap, account);
|
|
||||||
await SelectFolderAsync(imap, folder);
|
|
||||||
|
|
||||||
var mail = await FetchEmailByUidAsync(imap, uid, withAttachments, cancellationToken);
|
var mail = await FetchEmailByUidAsync(imap, uid, withAttachments, cancellationToken);
|
||||||
|
|
||||||
await imap.CloseAsync(cancellationToken);
|
await imap.CloseAsync(cancellationToken);
|
||||||
@@ -147,11 +141,9 @@ public class LimilabsImapEmailService(
|
|||||||
string folder = "INBOX",
|
string folder = "INBOX",
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var imap = new Imap();
|
using var imap = await OpenAsync(account, folder);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await ConnectAndAuthenticateAsync(imap, account);
|
|
||||||
await SelectFolderAsync(imap, folder);
|
|
||||||
await imap.MarkMessageSeenByUIDAsync(uid, cancellationToken);
|
await imap.MarkMessageSeenByUIDAsync(uid, cancellationToken);
|
||||||
await imap.CloseAsync(cancellationToken);
|
await imap.CloseAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
@@ -172,13 +164,9 @@ public class LimilabsImapEmailService(
|
|||||||
// Private helpers
|
// Private helpers
|
||||||
private async Task<List<long>> FetchEmailUidsAsync(
|
private async Task<List<long>> FetchEmailUidsAsync(
|
||||||
Imap imap,
|
Imap imap,
|
||||||
EmailAccountDto account,
|
|
||||||
MailSearchFilter filter,
|
MailSearchFilter filter,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
await ConnectAndAuthenticateAsync(imap, account);
|
|
||||||
await SelectFolderAsync(imap, filter.Folder);
|
|
||||||
|
|
||||||
List<ICriterion> criterions = [];
|
List<ICriterion> criterions = [];
|
||||||
|
|
||||||
if (filter.UnseenOnly)
|
if (filter.UnseenOnly)
|
||||||
@@ -286,8 +274,10 @@ public class LimilabsImapEmailService(
|
|||||||
return withAttachments || email is null ? email : email with { Attachments = [] };
|
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)
|
if (account.ImapUseSsl)
|
||||||
await imap.ConnectSSLAsync(account.ImapServer!, account.ImapPort);
|
await imap.ConnectSSLAsync(account.ImapServer!, account.ImapPort);
|
||||||
else
|
else
|
||||||
@@ -298,13 +288,12 @@ public class LimilabsImapEmailService(
|
|||||||
: account.Password;
|
: account.Password;
|
||||||
|
|
||||||
await imap.LoginAsync(account.Username, password);
|
await imap.LoginAsync(account.Username, password);
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task SelectFolderAsync(Imap imap, string folder)
|
|
||||||
{
|
|
||||||
if (string.Equals(folder, "INBOX", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(folder, "INBOX", StringComparison.OrdinalIgnoreCase))
|
||||||
await imap.SelectInboxAsync();
|
await imap.SelectInboxAsync();
|
||||||
else
|
else
|
||||||
await imap.SelectAsync(folder);
|
await imap.SelectAsync(folder);
|
||||||
|
|
||||||
|
return imap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user