• Loading...
ComponentPro UltimateMail

Connecting and authenticating to an IMAP server Asynchronously

Language Filter: AllSend comments on this topic to ComponentPro

Use ConnectAsync and AuthenticateAsync to asynchronously connect and login to the IMAP server. These methods establish a connection and authenticate user to the server asynchronously with execution occurring on a new thread, therefore they allow your next line of code to execute immediately.

In Task-based Asynchronous programs, the single asynchronous method represents the initiation and completion of an asynchronous operation. You may create the continuation code either explicitly, through methods on the Task class (for example, ContinueWith) or implicitly, by using language support built on top of continuations (for example, await in C#, Await in Visual Basic).

In .NET 4.5 it is recommended to implement the Task-based Asynchronous Pattern.

In Event-based Asynchronous programs, the event ConnectCompleted is raised when the ConnectAsync completes and the event AuthenticateCompleted is raised when the AuthenticateAsync completes. In the event handler method of the ConnectCompleted, you can check if there were any errors by using the Error property of the event data object.

The following example demonstrates how to connect and authenticate to an IMAP server asynchronously:

Task-based approach

using System;
using System.ComponentModel;
using System.Text;
using ComponentPro.Net.Mail;
using ComponentPro;

...

// Create a new instance.
Imap client = new Imap();

try
{
    // Connect to the IMAP server asynchronously. 
    await client.ConnectAsync("myserver");

    // Or you can specify the IMAP port with 
    // await client.ConnectAsync("myserver", 143); 
 
    // Authenticate the user asynchronously. 
    await client.AuthenticateAsync("userName", "password");

    // Do something here... 
    StringBuilder sb = new StringBuilder();

    FolderCollection list = client.ListFolders();
    for (int i = 0; i < list.Count; i++)
    {
        sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
    }

    Console.WriteLine(sb.ToString());

    // ... 
 
    // Disconnect asynchronously. 
    await client.DisconnectAsync();
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex.ToString());
}

Event-based approach

using System;
using System.ComponentModel;
using System.Text;
using ComponentPro.Net.Mail;
using ComponentPro;

...

public void DoAsyncConnect()
{
    // Create a new instance. 
    Imap client = new Imap();

    client.ConnectCompleted += client_ConnectCompleted;
    client.AuthenticateCompleted += client_AuthenticateCompleted;
    client.DisconnectCompleted += client_DisconnectCompleted;

    // Connect to the IMAP server asynchronously. 
    client.ConnectAsync("myserver");

    // Or you can specify the IMAP port with 
    // client.ConnectAsync("myserver", 143); 
 
    // Your code here 
    // ...
}

void client_ConnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
{
    Imap client = (Imap)sender;
    if (e.Error != null)
    {
        Console.WriteLine("Error: " + e.Error.ToString());
        return;
    }

    // Authenticate the user asynchronously. 
    client.AuthenticateAsync("userName", "password");
}

void client_AuthenticateCompleted(object sender, AsyncCompletedEventArgs e)
{
    Imap client = (Imap)sender;
    if (e.Error != null)
    {
        Console.WriteLine("Error: " + e.Error.ToString());
        return;
    }

    // Do something here... 
    StringBuilder sb = new StringBuilder();

    FolderCollection list = client.ListFolders();
    for (int i = 0; i < list.Count; i++)
    {
        sb.AppendFormat("{0} - {1}\r\n", i + 1, list[i].Name);
    }

    Console.WriteLine(sb.ToString());

    // ... 
 
    // Disconnect asynchronously. 
    client.DisconnectAsync();
}

void client_DisconnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
{
    Imap client = (Imap)sender;
    if (e.Error != null)
    {
        Console.WriteLine("Error: " + e.Error.ToString());
    }
}