Working with Pop3Client Asynchronously

Working with messages can also be performed asynchronously using the Aspose.Email Pop3Client. This article shows how to retrieve messages from a mailbox asynchronously. It also shows how to list messages by providing search criteria using MailQuery. It will be shown separately how to interrupt an operation with a mailbox started by a task-based asynchronous pattern (TAP) method.

Retrieve Messages Asynchronously

The following code snippet shows you how to retrieve messages asynchronously.

List Messages Asynchronously with MailQuery

The MailQuery class can be used to specify search criteria for retrieving a list of messages asynchronously as is shown in the following code sample.

How to Interrupt a TAP Method

Starting with .NET Framework 4.5, you can use asynchronous methods implemented according to TAP model. The code snippet below shows how to receive information about a mailbox using the task-based asynchronous pattern method named GetMailboxInfoAsync and then interrupt this process after a while.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

using (Pop3Client client = new Pop3Client(host, 995, senderEmail, password, SecurityOptions.Auto))
{
    CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);
    Exception exception = null;

    ThreadPool.QueueUserWorkItem(delegate
    {
        try
        {
            // start receiving mailbox information
            var task = client.GetMailboxInfoAsync(cancellationTokenSource.Token);
            Pop3MailboxInfo mailboxInfo = task.GetAwaiter().GetResult();
            Console.WriteLine("Message count: " + mailboxInfo.MessageCount);
        }
        catch (Exception e)
        {
            exception = e;
        }
        finally
        {
            autoResetEvent.Set();
        }
    });

    Thread.Sleep(2000);

    // stop receiving mailbox information
    cancellationTokenSource.Cancel();
    autoResetEvent.WaitOne();

    if (exception is OperationCanceledException)
        Console.WriteLine("Operation has been interrupted: " + exception.Message);
}