Skip to content

Commit

Permalink
added GetValidUniqueIdsAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Sep 14, 2023
1 parent 4c2cd62 commit ed5c2f0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
6 changes: 5 additions & 1 deletion samples/WorkerServiceExample/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken =
{
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
//await GetMessageSummaryRepliesAsync(cancellationToken);
await ReceiveAsync(cancellationToken);
//await ReceiveAsync(cancellationToken);
//await QueryAsync(cancellationToken);
//await MonitorAsync(cancellationToken);
//await DeleteSeenAsync(cancellationTokenSource);
//await NotReentrantAsync(cancellationToken);
//await SendAttachmentAsync(500);
//await DownloadAllAttachmentsAsync(cancellationToken);
//await TemplateSendAsync();
await ReceiveMimeMessagesContinuouslyAsync(
UniqueId.MinValue, new UniqueId(UniqueId.MinValue.Id + 10),
(m, ct) => { _logger.LogInformation(m.MessageId); return Task.CompletedTask; },
cancellationToken);
}

private async Task DownloadEmailAsync(string filePath = "download.eml", CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="4.1.0" />
<PackageReference Include="MimeKit" Version="4.1.0" />
<PackageReference Include="MailKit" Version="4.2.0" />
<PackageReference Include="MimeKit" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
Expand Down
41 changes: 33 additions & 8 deletions source/MailKitSimplified.Receiver/Services/MailFolderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public IMailReader Items(MessageSummaryItems messageSummaryItems)
return this;
}

private static async Task<IEnumerable<UniqueId>> GetValidUniqueIdsAsync(IMailFolder mailFolder, IEnumerable<UniqueId> uniqueIds, CancellationToken cancellationToken = default)
{
var ascendingIds = uniqueIds is IList<UniqueId> ids ? ids : new UniqueIdSet(uniqueIds, SortOrder.Ascending);
var messageSummaries = await mailFolder.FetchAsync(ascendingIds, MessageSummaryItems.UniqueId, cancellationToken).ConfigureAwait(false);
return messageSummaries.Select(m => m.UniqueId);
}

private async Task<(IMailFolder, bool)> OpenMailFolderAsync(CancellationToken cancellationToken = default)
{
if (_take == 0)
Expand Down Expand Up @@ -310,13 +317,21 @@ public async Task<IList<MimeMessage>> GetMimeMessagesAsync(IEnumerable<UniqueId>
if (uniqueIds != null)
{
(var mailFolder, var closeWhenFinished) = await OpenMailFolderAsync(cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in uniqueIds)
var filteredUids = await GetValidUniqueIdsAsync(mailFolder, uniqueIds, cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in filteredUids)
{
if (cancellationToken.IsCancellationRequested)
break;
var mimeMessage = await mailFolder.GetMessageAsync(uniqueId, cancellationToken, progress).ConfigureAwait(false);
if (mimeMessage != null)
mimeMessages.Add(mimeMessage);
try
{
var mimeMessage = await mailFolder.GetMessageAsync(uniqueId, cancellationToken, progress).ConfigureAwait(false);
if (mimeMessage != null)
mimeMessages.Add(mimeMessage);
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"{_imapReceiver} failed to get message #{uniqueId}.");
}
}
_logger.LogTrace($"{_imapReceiver} received {mimeMessages.Count} email(s).");
await CloseMailFolderAsync(mailFolder, closeWhenFinished, mimeMessages.Count).ConfigureAwait(false);
Expand Down Expand Up @@ -415,12 +430,21 @@ public async IAsyncEnumerable<MimeMessage> GetMimeMessages(IEnumerable<UniqueId>
if (uniqueIds != null)
{
(var mailFolder, var closeWhenFinished) = await OpenMailFolderAsync(cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in uniqueIds)
var filteredUids = await GetValidUniqueIdsAsync(mailFolder, uniqueIds, cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in filteredUids)
{
if (cancellationToken.IsCancellationRequested)
break;
var mimeMessage = await mailFolder.GetMessageAsync(uniqueId, cancellationToken, progress).ConfigureAwait(false);
_logger.LogTrace($"{_imapReceiver} received {mimeMessage.MessageId}.");
MimeMessage mimeMessage = null;
try
{
mimeMessage = await mailFolder.GetMessageAsync(uniqueId, cancellationToken, progress).ConfigureAwait(false);
_logger.LogTrace($"{_imapReceiver} received {mimeMessage.MessageId}.");
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"{_imapReceiver} failed to get message #{uniqueId}.");
}
if (mimeMessage != null)
yield return mimeMessage;
}
Expand Down Expand Up @@ -472,7 +496,8 @@ public async Task SaveAllAsync(IEnumerable<UniqueId> uniqueIds, string folderPat
var format = FormatOptions.Default.Clone();
format.NewLineFormat = NewLineFormat.Dos;
(var mailFolder, var closeWhenFinished) = await OpenMailFolderAsync(cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in uniqueIds)
var filteredUids = await GetValidUniqueIdsAsync(mailFolder, uniqueIds, cancellationToken).ConfigureAwait(false);
foreach (var uniqueId in filteredUids)
{
if (cancellationToken.IsCancellationRequested)
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="4.1.0" />
<PackageReference Include="MimeKit" Version="4.1.0" />
<PackageReference Include="MailKit" Version="4.2.0" />
<PackageReference Include="MimeKit" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
Expand Down

0 comments on commit ed5c2f0

Please sign in to comment.