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.
- Add the Smtp component to a new form.
- 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 Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- 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.
- 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)
- 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 Codeprivate 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 CodePrivate 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
- Compile and run the application.