It is often desirable, when your applications perform actions that could take longer than a few seconds, to display the progress of the action to the user. The Smtp component fully supports progress notification through the Progress event. The Progress event will be raised periodically while data transfer is in progress, making accessible necessary data to display progress information, such as the length of the data and the current position within the data.
To display progress when sending a message.
- 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.
- Create a message with a large attachment. Using a large attachment will illustrate the Progress event more clearly, as progress notification will occur over a longer period of time. Place the following code in the Click event of the button added to the form in step 3.
C#
Copy Code// Create a message MessageStream msg = new MessageStream(); msg.To.Add(new MailAddress("you@yourserver.com")); msg.From = new MailAddress("me@myserver.com"); msg.Subject = "hello"; msg.Text = "This is a test"; // Add a large attachment so progress will be more easily visible. msg.Attachments.Add("C:\\temp\\bigfile.bmp");
Visual Basic
Copy Code' Create a message Dim msg As New MessageStream() msg.To.Add(New MailAddress("jeff@dart.com")) msg.From = New MailAddress("me@myserver.com") msg.Subject = "hello" msg.Text = "This is a test" ' Add a large attachment so progress will be more easily visible. msg.Attachments.Add("C:\\temp\\bigfile.bmp")
- Add code to send the message.
C#
Copy Code// Send the message smtp1.Server = "mail.test.com"; smtp1.Send(msg);
Visual Basic
Copy Code' Send the message Smtp1.Server = "mail.test.com" Smtp1.Send(msg)
- The Progress event will be raised whenever progress information is available. "Wire-up" the Progress event. For more information on how to do this, see Using Events In PowerTCP.
- Add a ProgressBar object.
- Add code to the Progress event to use the ProgressBar object to reflect the status of the send operation. Add the following code to your Progress event handler. When finished, your Progress event handler should look similar to the following.
C#
Copy Codeprivate void smtp1_Progress(object sender, Dart.PowerTCP.Mail.ProgressEventArgs e) { // Set the ProgressBar to reflect the current data position. progressBar1.Minimum = 0; progressBar1.Maximum = (int)e.Length; progressBar1.Value = (int)e.Position; }
Visual Basic
Copy CodePrivate Sub Pop1_Progress(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.PopProgressEventArgs) Handles Pop1.Progress ' Set the ProgressBar to reflect the current data position. ProgressBar1.Minimum = 0 ProgressBar1.Maximum = e.Length ProgressBar1.Value = e.Position End Sub
- Compile and run the application.