• Loading...
ComponentPro UltimateMail

Sending a message Asynchronously

Language Filter: AllSend comments on this topic to ComponentPro

Use SendAsync methods to asynchronously send a message. These methods send a message 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 SendCompleted is raised when the SendAsync method completes. In the event handler method of the SendCompleted, you can check if there were any errors by using the Error property of the event data object.

The following example demonstrates how to asynchronously send a message:

Task-based approach

using System;
using ComponentPro.Net.Mail;

...

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

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

            // Or you can specify the SMTP port with 
            // client.Connect("myserver", 25); 
 
            // Login to the server. 
            client.Authenticate("user", "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";

            // Send the message. 
            await client.SendAsync(msg);

            // ... 
 
            Console.WriteLine("Message sent successfully.");

            // Disconnect. 
            client.Disconnect();

Event-based approach

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

...

public void DoSendAsync()
{
    // Create a new instance of the Smtp class. 
    Smtp client = new Smtp();

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

    // Or you can specify the SMTP port with 
    // client.Connect("myserver", 25); 
 
    // Login to the server. 
    client.Authenticate("user", "password");

    // ... 
 
    // Register an event handler. 
    client.SendCompleted += client_SendCompleted;

    // 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";

    // Send the message. 
    client.SendAsync(msg);

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

void client_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
    // Smtp client = (Smtp)sender; 
 
    if (e.Error != null)
        Console.WriteLine("Error: " + e.Error.ToString());
    else 
        Console.WriteLine("Message sent successfully.");
}