ComponentPro UltimateMail

Connecting and authenticating to an SMTP server Asynchronously

Language Filter: AllSend comments on this topic to ComponentPro

Use ConnectAsync and AuthenticateAsync to asynchronously connect and login to the SMTP 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 SMTP server asynchronously:

Task-based approach

using System;
using ComponentPro.Net.Mail;
using ComponentPro;

...

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

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

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

// Create a new mail message.
MailMessage msg = new MailMessage();
msg.Subject = "Test Subject";
msg.BodyText = "Content";
msg.From = "from@mydomain.com";
msg.To = "to@somedomain.com";

// And send it.
client.Send(msg);

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

// ...

Event-based approach

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

...

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

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

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

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

void client_ConnectCompleted(object sender, ExtendedAsyncCompletedEventArgs<string> e)
{
    Smtp client = (Smtp)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)
{
    Smtp client = (Smtp)sender;
    if (e.Error != null)
    {
        Console.WriteLine("Error: " + e.Error.ToString());
        return;
    }

    // Create a new mail message. 
    MailMessage msg = new MailMessage();
    msg.Subject = "Test Subject";
    msg.BodyText = "Content";
    msg.From = "from@mydomain.com";
    msg.To = "to@somedomain.com";

    // And send it. 
    client.Send(msg);

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

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