РУС Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal   

Asynchronous mail Send / Receive in separated Thread

Submitted on May 16, 2006, Updated on February 8, 2007, Updated on October 15, 2011

Introduction

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:

procedure TclMailThread.Execute;
var
   dwResult: DWORD;
   arr: array[0..1] of THandle;
begin
   arr[0] := FStopEvent;
   arr[1] := FStartEvent;

   repeat
      dwResult := WaitForMultipleObjects(2, @arr, FALSE, INFINITE);
      if (dwResult = WAIT_OBJECT_0 + 1) then
      begin
         FAsyncMethod();
      end;
   until Terminated or (dwResult = WAIT_OBJECT_0);

end;

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:

procedure TclMailThread.SendMail(ASmtp: TclSmtp; AMessage: TclMailMessage);
begin
   FAsyncMethod := AsyncSendMail;
   FSmtp := ASmtp;
   FMailMessage := AMessage;
   SetEvent(FStartEvent);
end;

procedure TclMailThread.AsyncSendMail;
begin
   if FSmtp.Active then Exit;

   FSmtp.Open();
   FSmtp.Send(FMailMessage);
   FSmtp.Close();

   FSynchronizer.Synchronize(DoSent);
end;

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.

Best regards,
Sergey Shirokov 
Clever Components team

    Copyright © 2000-2017