Asynchronous mail Send / Receive in separated Thread
Submitted on May 16, 2006, Updated on February 8, 2007, Updated on October 15, 2011
Often the process of working with email messages takes lots of time. The time is spent on downloading big email attachments, checking email accounts which have lots of email messages, sending email messages via low-speed connection and so on. If you run GUI application, such delays may hang the application interface and the user can not do his job with this program.
In this article I have used one possible solution - the multithreaded approach for working with emails in your application.
There are two common tasks should be solved when using threads in your application: running time and resources consuming functions asynchronously and synchronizing the results with the main application thread.
I have used the Clever Internet Suite v 6.0 Internet components since the Clever Internet Suite is fully thread-safe and allows implementing the most possible email tasks. For synchronizing the application threads let's use a special ThreadSynchronizer class from the Clever Internet Suite v 6.0 library.
Clever Internet Suite 7.3 edition of SmtpPopAsync demo can be downloaded here: Download SmtpPopAsync sample
Creating a worker thread
First, let's go ahead and create a special working thread class and implement the thread procedure:
Synchronizing the worker thread with the main application thread
The code below demonstrates how to use the ThreadSynchronizer class for synchronizing the working thread and the main application thread:
The approach of processing emails in asynchronous mode can be successfully used for both POP3 and SMTP protocols, the IMAP4 or event for HotMail service. The Clever Internet Suite v 6.0 allows you to implement all these protocols using the corresponding components from this library. The code from this article can be easily improved and extended for using with any other Internet protocol or even with any other Internet library.
Download Source Code
This code is constantly refined and improved and your comments and suggestions are always welcome.
Please feel free to Contact Us It will be our pleasure to answer your questions.