Site Map Email Us Home
 
All Articles
Subscribe and receive email notifications on all major releases and other important events.
Your Name:
Your Email:
 
Latest News

Access repair download
Repair Database Files




Best Offer for Delphi / C++ Builder Developers

 

Buy FIBPlus at discount price !

 
 
Products Articles Downloads Order Support

Asynchronous mail Send / Receive in separated Thread

Submitted 16 May 2006, Updated 8 February 2007

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 email 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.

Creating working 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 working 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 me at info@clevercomponents.com. It will be my pleasure to answer to your questions.

Best regards,
Sergey Shirokov 
Clever Components team.
www.clevercomponents.com

 
Home  | Site Map  | Products  | Articles  | Downloads  | Order  | Support
 
    Copyright © 2000-2010