Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

September 25, 2007

C# SMTP Send in Background Thread with Progress Monitoring and Abort

This blog post provides a C# example in Visual Studio 2005 showing how to send an email in a background thread using the .NET BackgroundWorker class. The example shows a progress bar (0% to 100%) that updates while the background send is in progress. The send can be aborted at any point by clicking on an "Abort" button. There is also a hearbeat progress bar that updates every .1 seconds.

Here is a screenshot of the example:
(The listbox showing Item 1, 2, …, etc. and the checkboxes only exist to provide UI controls that can be tested while the email sending is in progress. You should be able to scroll the listbox, check and uncheck the checkboxes, etc. while the email send is in progress and while the progress bars are updating.)
SMTP Background Thread with Progress

The VS2005 project files can be downloaded from here: SMTP Background Thread Example.

We’ve explicity did not include the ChilkatDotNet2.dll within the download (because it could be out-of-date at a future date). Be sure to remove and re-add the reference to the Chilkat .NET assembly.

Also: You cannot simply build the project and run it. You’ll need to first edit the code to insert your own SMTP server hostname, login, and password. These lines will need to be changed:

            m_mailman.SmtpHost = "mail.chilkatsoft.com";
            m_mailman.SmtpUsername = "****";
            m_mailman.SmtpPassword = "****";

(Depending on your situation, you may or may not need to include the SmtpUsername and SmtpPassword.)

The example code is extensively documented. A few major points:

  • When testing, make sure the email you send is large enough so you can test aborting it. We’ve included and added a few relatively large attachments, but if your Internet connection is too fast, or if you’re connecting to an SMTP server on localhost, it may be too quick for you to abort.
  • The program uses the BackgroundWorker class (part of the .NET Framework) to run the SendEmail method in a background thread. The BackgroundWorker class is special in that its event handlers are allowed to update controls on the foreground Form. Therefore, the email component’s AbortCheck and SendPercentDone events in turn fire the BackgroundWorker’s ProgressChanged event so that it can update the progress bars. This is a design pattern that could be used with any component having event callbacks..


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.