Skip to content

Commit

Permalink
Fixed cached source folder in move method #70
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Aug 5, 2024
1 parent d0acdd1 commit d03f07a
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 89 deletions.
10 changes: 10 additions & 0 deletions samples/WorkerServiceExample/EmailWorkerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MailKitSimplified.Sender.Models
{
public class EmailWorkerOptions
{
public const string SectionName = "EmailWorker";

public string DefaultFromAddress { get; set; } = "noreply@localhost";
public string DefaultToAddress { get; set; } = "noreply@localhost";
}
}
3 changes: 3 additions & 0 deletions samples/WorkerServiceExample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MailKitSimplified.Sender;
using MailKitSimplified.Receiver;
using MailKitSimplified.Sender.Models;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
Expand All @@ -8,6 +9,8 @@
//services.AddMailKitSimplifiedEmail(context.Configuration);
services.AddScopedMailKitSimplifiedEmailSender(context.Configuration);
services.AddScopedMailKitSimplifiedEmailReceiver(context.Configuration);
var workerSection = context.Configuration.GetRequiredSection(EmailWorkerOptions.SectionName);
services.Configure<EmailWorkerOptions>(workerSection);
})
.Build();

Expand Down
56 changes: 25 additions & 31 deletions samples/WorkerServiceExample/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using MailKitSimplified.Receiver.Extensions;
using MailKitSimplified.Receiver.Services;
using MailKitSimplified.Sender.Abstractions;
using MailKitSimplified.Sender.Models;
using Microsoft.Extensions.Options;

namespace ExampleNamespace;

Expand Down Expand Up @@ -47,8 +49,8 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken =
//await GetMailFolderCacheAsync();
//await CreateFolderAndMoveTopOneAsync();
//await MonitorAsync(cancellationToken);
//await MonitorMoveAsync(cancellationToken);
//await MoveToAsync("Process", 2, cancellationToken);
//await MoveToAsync(_processed, 1, cancellationToken);
await MonitorMoveAsync(cancellationToken);
}

private static ImapReceiver CreateExchangeOAuth2ImapClientExample(SaslMechanismOAuth2 oauth2)
Expand Down Expand Up @@ -84,8 +86,9 @@ private async Task MailFolderMonitorMoveFolderAsync(string destinationFolderFull
{
using var mailFolderClient = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderClient>();
var mailFolderMonitorFactory = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderMonitorFactory>();
var destinationFolder = await mailFolderClient.GetFolderAsync(_processed, createIfNotFound: true, cancellationToken);
async Task UniqueIdArrivedAsync(IMessageSummary messageSummary) =>
await mailFolderClient.MoveToAsync(messageSummary, destinationFolderFullName, cancellationToken);
await mailFolderClient.MoveToAsync(messageSummary, destinationFolder, cancellationToken);
await mailFolderMonitorFactory.MonitorAllMailboxesAsync(UniqueIdArrivedAsync, cancellationToken);
}

Expand Down Expand Up @@ -132,7 +135,8 @@ private async Task MoveSeenToSentAsync(CancellationTokenSource cancellationToken
private async Task AddToDraftFolderAsync()
{
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
var mimeMessage = CreateTemplate(smtpSender).MimeMessage;
var options = _serviceScope.ServiceProvider.GetRequiredService<IOptions<EmailWorkerOptions>>().Value;
var mimeMessage = CreateTemplate(smtpSender, options.DefaultFromAddress, options.DefaultToAddress).MimeMessage;
var draftsFolder = _imapReceiver.MailFolderClient.DraftsFolder;
var uniqueId = await draftsFolder.AppendAsync(mimeMessage);
_logger.LogInformation($"Added mime message to {_imapReceiver} {draftsFolder.FullName} folder as #{uniqueId}.");
Expand All @@ -144,7 +148,7 @@ private async Task MoveTopOneToFolderAsync(IMailFolderClient mailFolderClient, s
var uniqueId = await mailFolderClient.MoveToAsync(messageSummary, destinationFolderFullName, cancellationToken);
//var destinationFolder = await mailFolderClient.GetFolderAsync([destinationFolderFullName], cancellationToken);
//var uniqueId = await messageSummary.MoveToAsync(destinationFolder, cancellationToken);
_logger.LogInformation($"Moved {_imapReceiver} mime message {messageSummary.UniqueId} to {destinationFolderFullName} folder as #{uniqueId}.");
_logger.LogInformation($"Moved {_imapReceiver} mime message #{messageSummary.UniqueId} to {destinationFolderFullName} folder as #{uniqueId}.");
}

private async Task CreateFolderAndMoveTopOneAsync(string mailFolderFullName = _processed, CancellationToken cancellationToken = default)
Expand All @@ -157,19 +161,6 @@ private async Task CreateFolderAndMoveTopOneAsync(string mailFolderFullName = _p
await MoveTopOneToFolderAsync(mailFolderClient, mailFolderFullName, cancellationToken);
}

private async Task GetMailFolderCacheAsync(string mailFolderFullName = _processed, CancellationToken cancellationToken = default)
{
using var mailFolderClient = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderClient>();
var mailFolderCache = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderCache>();
var folder = await mailFolderCache.GetMailFolderAsync(_imapReceiver, mailFolderFullName, createIfMissing: true, cancellationToken);
_logger.LogInformation(folder.FullName);
var messageSummary = await GetTopMessageSummaryAsync(cancellationToken);
var uniqueId = await mailFolderCache.MoveToAsync(_imapReceiver, messageSummary, mailFolderFullName, cancellationToken);
//var destinationFolder = await mailFolderClient.GetFolderAsync([destinationFolderFullName], cancellationToken);
//var uniqueId = await messageSummary.MoveToAsync(destinationFolder, cancellationToken);
_logger.LogInformation($"Moved {_imapReceiver} mime message #{messageSummary.UniqueId} to {mailFolderFullName} folder as #{uniqueId}.");
}

public async Task GetMailFolderAsync(string mailFolderFullName, CancellationToken cancellationToken = default)
{
var messageSummary = await GetTopMessageSummaryAsync(cancellationToken);
Expand Down Expand Up @@ -394,14 +385,15 @@ private async Task QueryAsync(CancellationToken cancellationToken = default)
_logger.LogInformation($"{_imapReceiver} received {messageSummaries.Count} email(s) in {stopwatch.Elapsed.TotalSeconds:n1}s: {messageSummaries.Select(m => m.UniqueId).ToEnumeratedString()}.");
}

private IEmailWriter CreateTemplate(ISmtpSender smtpSender, string from = "[email protected]")
private IEmailWriter CreateTemplate(ISmtpSender smtpSender, string from = "[email protected]", string? to = null)
{
if (!from.IsEmail())
_logger.LogWarning($"{from} is not a valid email.");
var id = $"{Guid.NewGuid():N}"[..8];
to ??= $"{id}@localhost";
var template = smtpSender.WriteEmail
.From(from)
.To($"{id}@localhost")
.To(to)
.Subject(id)
.BodyText("text/plain.")
.BodyHtml("text/html.")
Expand All @@ -412,9 +404,10 @@ private IEmailWriter CreateTemplate(ISmtpSender smtpSender, string from = "me@ex
private async Task TemplateSendAsync(byte numberToSend = 1, CancellationToken cancellationToken = default)
{
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
var options = _serviceScope.ServiceProvider.GetRequiredService<IOptions<EmailWorkerOptions>>().Value;
//var template = await GetTemplate().SaveTemplateAsync();
//var template = await smtpSender.WithTemplateAsync();
var template = CreateTemplate(smtpSender);
var template = CreateTemplate(smtpSender, options.DefaultFromAddress, options.DefaultToAddress);
int count = 0;
do
{
Expand All @@ -428,19 +421,21 @@ private async Task TemplateSendAsync(byte numberToSend = 1, CancellationToken ca
private async Task SendAttachmentAsync(int millisecondsDelay, string filePath = "..\\..\\README.md", CancellationToken cancellationToken = default)
{
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
bool isSent = await CreateTemplate(smtpSender)
var options = _serviceScope.ServiceProvider.GetRequiredService<IOptions<EmailWorkerOptions>>().Value;
bool isSent = await CreateTemplate(smtpSender, options.DefaultFromAddress, options.DefaultToAddress)
.TryAttach(filePath)
.TrySendAsync(cancellationToken);
_logger.LogInformation($"Email {(isSent ? "sent" : "failed to send")}.");
await Task.Delay(millisecondsDelay, cancellationToken);
}

private async Task DelayedSendAsync(int millisecondsDelay, ISmtpSender? smtpSender = null, CancellationToken cancellationToken = default)
private async Task DelayedSendAsync(TimeSpan delay, ISmtpSender? smtpSender = null, CancellationToken cancellationToken = default)
{
var temporarySender = smtpSender == null;
smtpSender ??= _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
await Task.Delay(millisecondsDelay, cancellationToken);
smtpSender.Enqueue(CreateTemplate(smtpSender).MimeMessage);
var options = _serviceScope.ServiceProvider.GetRequiredService<IOptions<EmailWorkerOptions>>().Value;
await Task.Delay(delay, cancellationToken);
smtpSender.Enqueue(CreateTemplate(smtpSender, options.DefaultFromAddress, options.DefaultToAddress).MimeMessage);
//bool isSent = await CreateTemplate(smtpSender).TrySendAsync(cancellationToken);
//_logger.LogInformation($"Email {(isSent ? "sent" : "failed to send")}.");
if (temporarySender) smtpSender.Dispose();
Expand All @@ -450,7 +445,7 @@ private async Task NotReentrantAsync(CancellationToken cancellationToken = defau
{
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
var sendTask = DelayedSendAsync(500, smtpSender, cancellationToken);
var sendTask = DelayedSendAsync(TimeSpan.FromMilliseconds(500), smtpSender, cancellationToken);
var newestEmail = await GetNewestMessageSummaryAsync();
await _imapReceiver.MonitorFolder.SetMessageSummaryItems()
.SetIgnoreExistingMailOnConnect()
Expand Down Expand Up @@ -487,11 +482,10 @@ private async Task MonitorMoveAsync(CancellationToken cancellationToken = defaul
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
using var mailFolderClient = _serviceScope.ServiceProvider.GetRequiredService<IMailFolderClient>();
int delayMs = 1000;
var sendTasks = new List<Task>();
for (int waitCount = 1; waitCount <= 2; waitCount++)
{
var sendTask = DelayedSendAsync(waitCount * delayMs, smtpSender, cancellationToken);
var sendTask = DelayedSendAsync(TimeSpan.FromSeconds(waitCount+4), smtpSender, cancellationToken);
sendTasks.Add(sendTask);
}
var destinationFolder = await mailFolderClient.GetFolderAsync(_processed, createIfNotFound: true, cancellationToken);
Expand All @@ -505,18 +499,18 @@ await _imapReceiver.MonitorFolder

async Task ProcessMessageAsync(IMessageSummary messageSummary)
{
var uniqueId = await mailFolderClient.MoveToAsync(messageSummary.UniqueId, destinationFolder, cancellationToken);
var uniqueId = await mailFolderClient.MoveToAsync(messageSummary, destinationFolder, cancellationToken);
if (uniqueId == null)
_logger.LogInformation($"{_imapReceiver} message #{messageSummary.UniqueId} not moved to [{_processed}], UniqueId is null.");
else
_logger.LogDebug($"{_imapReceiver} message #{messageSummary.UniqueId} moved to [{_processed}] {uniqueId}.");
_logger.LogDebug($"{_imapReceiver} message #{messageSummary.UniqueId} moved to [{_processed}] #{uniqueId}.");
}
}

private async Task MonitorAsync(CancellationToken cancellationToken = default)
{
using var smtpSender = _serviceScope.ServiceProvider.GetRequiredService<ISmtpSender>();
var sendTask = DelayedSendAsync(500, smtpSender, cancellationToken);
var sendTask = DelayedSendAsync(TimeSpan.FromMilliseconds(500), smtpSender, cancellationToken);
void ProcessMessage(IMessageSummary messageSummary) =>
_logger.LogInformation($"{_imapReceiver} message #{messageSummary.UniqueId} processed.");
await _imapReceiver.MonitorFolder
Expand Down
4 changes: 4 additions & 0 deletions samples/WorkerServiceExample/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"ImapHost": "localhost",
"ImapPort": 143
},
"EmailWorker": {
"DefaultFromAddress": "[email protected]",
"DefaultToAddress": "[email protected]"
},
"Mailbox": {
"EmailReceivers": [
{
Expand Down
Loading

0 comments on commit d03f07a

Please sign in to comment.