PowerTCP Mail for .NET
Sending Mail Asynchronously
Send comments on this topic.



Glossary Item Box

Asynchronous operations are useful because they occur without waiting for the action to complete before executing the next line of code. Such a technique is useful when an operation is time consuming and other code needs to execute without waiting for the operation to complete.

To send a message asynchronously.

  1. Add the Smtp component to a new form.
  2. Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
    C# Copy Code
    using Dart.PowerTCP.Mail;
    Visual Basic Copy Code
    Imports Dart.PowerTCP.Mail
  3. Add a button to the form.
  4. In this example, the BeginSend method will be used. The EndSend event will be raised when this operation has completed. "Wire-up" the EndSend event. 
  5. Use BeginSend to send a quick message. This operation will take place on a new thread. When it has completed the EndSend event will be raised to notify the application. Place the following code in the Click event of the button added to the form in step 3.
    C# Copy Code
    // Set server
    smtp1.Server = "mail.test.com";
    
    // Begin to send a message.
    smtp1.BeginSend("you@yourserver.com", "me@myserver.com", "asynch test", "This is a test", null);
    Visual Basic Copy Code
    ' Set server
    Smtp1.Server = "mail.test.com"
    
    ' Begin to send a message.
    Smtp1.BeginSend("jeff@dart.com", "me@myserver.com", "asynch test", "This is a test", Nothing)
  6. When the EndSend event is raised, take some action. In this example, a message is displayed if no Exception is returned. Your entire EndSend event should look something like the following.
    C# Copy Code
    private void smtp1_EndSend(object sender, Dart.PowerTCP.Mail.SmtpEventArgs e)
    {
       // Check for any exceptions
       if(e.Exception == null)
          Debug.WriteLine("Mail successfully sent.");
    }
    Visual Basic Copy Code
    Private Sub Smtp1_EndSend(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.SmtpEventArgs) Handles Smtp1.EndSend
       ' Check for any exceptions
       If e.Exception Is Nothing Then
          Debug.WriteLine("Mail successfully sent.")
       End If
    End Sub
  7. Compile and run the application.
Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.