Skip to content

Commit

Permalink
#70 simplified the internal move methods. Also simplified IMailReader…
Browse files Browse the repository at this point in the history
….Top and Range to use int.
  • Loading branch information
danzuep committed Aug 1, 2024
1 parent 10ade21 commit 4abe3f3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 111 deletions.
17 changes: 14 additions & 3 deletions samples/WorkerServiceExample/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken =
//await GetMailFolderCacheAsync();
//await CreateFolderAndMoveTopOneAsync();
//await MonitorAsync(cancellationToken);
await MonitorMoveAsync(cancellationToken);
//await MonitorMoveAsync(cancellationToken);
//await MoveToAsync("Process", 2, cancellationToken);
}

private static ImapReceiver CreateExchangeOAuth2ImapClientExample(SaslMechanismOAuth2 oauth2)
Expand Down Expand Up @@ -88,6 +89,16 @@ async Task UniqueIdArrivedAsync(IMessageSummary messageSummary) =>
await mailFolderMonitorFactory.MonitorAllMailboxesAsync(UniqueIdArrivedAsync, cancellationToken);
}

private async Task MoveToAsync(string destinationFolderFullName = _processed, int howMany = 2, CancellationToken cancellationToken = default)
{
using var mailFolderClient = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderClient>();
var messageSummaries = await _imapReceiver.ReadMail.Top(howMany).GetMessageSummariesAsync(cancellationToken);
_logger.LogInformation($"{_imapReceiver} folder query returned {messageSummaries.Count} messages.");
var uniqueIds = messageSummaries.Select(m => m.UniqueId);
var movedUids = await mailFolderClient.MoveToAsync(uniqueIds, destinationFolderFullName, cancellationToken);
_logger.LogInformation($"Moved {movedUids.Count} {_imapReceiver} messages to {destinationFolderFullName}.");
}

private async Task DownloadEmailAsync(string filePath = "download.eml", CancellationToken cancellationToken = default)
{
var mimeMessage = await GetNewestMimeMessageAsync(cancellationToken);
Expand Down Expand Up @@ -485,6 +496,7 @@ private async Task MonitorMoveAsync(CancellationToken cancellationToken = defaul
var sendTask = DelayedSendAsync(waitCount * delayMs, smtpSender, cancellationToken);
sendTasks.Add(sendTask);
}
var destinationFolder = await mailFolderClient.GetOrCreateFolderAsync(_processed, cancellationToken);
await _imapReceiver.MonitorFolder
.SetMessageSummaryItems()
.SetIgnoreExistingMailOnConnect()
Expand All @@ -495,8 +507,7 @@ await _imapReceiver.MonitorFolder

async Task ProcessMessageAsync(IMessageSummary messageSummary)
{
var mailFolder = await mailFolderClient.GetOrCreateFolderAsync(_processed, cancellationToken);
var uniqueId = await mailFolderClient.MoveToAsync(messageSummary.UniqueId, mailFolder, cancellationToken);
var uniqueId = await mailFolderClient.MoveToAsync(messageSummary.UniqueId, destinationFolder, cancellationToken);
if (uniqueId == null)
_logger.LogInformation($"{_imapReceiver} message #{messageSummary.UniqueId} not moved to [{_processed}], UniqueId is null.");
else
Expand Down
4 changes: 2 additions & 2 deletions source/MailKitSimplified.Receiver/Abstractions/IMailReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface IMailReader
/// </summary>
/// <param name="count">Number of messages to return.</param>
/// <returns>Fluent <see cref="IMailReader"/>.</returns>
IMailReader Top(ushort count);
IMailReader Top(int count);

/// <summary>
/// Mail folder offset to start getting messages from, oldest to newest.
Expand Down Expand Up @@ -50,7 +50,7 @@ public interface IMailReader
/// <param name="batchSize">Zero-indexed batch size.</param>
/// <param name="continuous">Default is to continue in batches.</param>
/// <returns>Fluent <see cref="IMailReader"/>.</returns>
IMailReader Range(UniqueId start, ushort batchSize = 0, bool continuous = true);
IMailReader Range(UniqueId start, int batchSize = 0, bool continuous = true);

/// <summary>
/// Set a query for searching messages in a <see cref="IMailFolder"/>.
Expand Down
Loading

0 comments on commit 4abe3f3

Please sign in to comment.