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

Your Name:
Your Email:
Previous Newsletters

Products Articles Downloads Order Support
Customer Portal      

Email Server in Delphi with POP3, SMTP, and IMAP Support

Email Server in Delphi


The article represents a fully functional multithreaded multi connection Email server, which works asynchronously in a thread pool, accepts the POP3, IMAP, and SMTP connections, delivers E-mails via the SMTP protocol, and relays outgoing E-mails to end-recipients across the Web. The SSL/TLS encryption is available, as well.

This is a second article of the series, which introduces an updated GitHub project. A new graphic user interface allows users to configure the server, add new user accounts, manage SMTP, POP3, and IMAP protocols, and many more.

The Email Server is built based on the Clever Internet Suite library and uses the fast and stable components: TclSmtpServer, TclPop3Server, TclImap4Server, and TclSmtpRelay.


Accept Incoming SMTP, POP3, and IMAP Connections

The following three Delphi components implement the low-level Network communication via the SMTP, POP3, and IMAP4 protocols: TclSmtpServer, TclPop3Server, and TclImap4Server. All these components work in a thread pool and keep Emails in memory. Each component provides a set of events, which allow users to handle received data, incoming Emails, and manage user mailboxes. There are the following three Delphi components, which automatically handle all these events and store user data, mailboxes, and Emails on the disk: TclPop3FileHandler, TclIImap4FileHandler, and TclSmtpFileHandler.

All you need is to assign the Server property of the File Handler component with a corresponding server instance. After that, you can set up the components' properties, such as MailBox folder, server listening port, UserAccounts collection, and start the server. The SMTP File Handler component looks through incoming Emails and saves messages for local server users to the MailboxDir folder, and outgoing messages to the RelayDir folder. 


// [Delphi]
clSmtpServer1.Port := 25;

clSmtpFileHandler1.Server := clSmtpServer1;

clSmtpFileHandler1.MailBoxDir := 'C:\ CleverMailBox;

clSmtpFileHandler1.RelayDir := 'C:\CleverMailBox\RelayQueue';   



There are two file types: .MSG and .ENV. A file with .MSG extension keeps a message content, and .ENV file contains information about a message envelope: a sender and recipients. The other File Handler components, TclPop3FileHandler and TclImap4FileHandler, open the MailBoxDir folder, load and manage Emails.


// [Delphi]
clPop3Server1.Port := 110;

clPop3FileHandler1.Server := clPop3Server1;

clPop3FileHandler1.MailBoxDir := 'C:\ CleverMailBox;   


// [Delphi]
clImap4Server1.Port := 143;

clImap4FileHandler1.Server := clImap4Server1;

clImap4FileHandler1.MailBoxDir := 'C:\ CleverMailBox;   



Create User Accounts and Share between Server Components

The introduced project stores information about server settings and user accounts in a .ini file near the application executable. This project is mostly simple demos, and by no means, a completed application. You are free to modify this functionality and implement more secure storage for sensitive data.

Since all three server components should share the same user accounts, we can create an instance of the TclMailUserAccountList list and add each user account to this list. After that, we can set up the UserAccounts property of the TclSmtpServer, TclPop3Server, and TclImap4Server components:


// [Delphi]
UserAccounts := TclMailUserAccountList.Create(Self, TclMailUserAccountItem);   

count := Ini.ReadInteger('USERS', 'Count', 0);
for i := 0 to count - 1 do
   account := UserAccounts.Add();

   account.UserName := Ini.ReadString('USER' + IntToStr(i), 'UserName', '');
   account.Password := Ini.ReadString('USER' + IntToStr(i), 'Password', '');
   account.Email := account.UserName;

clSmtpServer1.UserAccounts := UserAccounts;

clPop3Server1.UserAccounts := UserAccounts;

clImap4Server1.UserAccounts := UserAccounts;


Relay Outgoing Email

As it was mentioned above, the TclSmtpFileHandler component saves outgoing Emails to the RelayDir folder. These messages should be delivered to external recipients on the Internet. The Clever Internet Suite provides a special TclSmtpRelay component, which loads the Email messages together with their envelopes, looks for MX servers using DNS lookup, and, finally, delivers the messages directly to recipients' mailboxes. An instance of the TTimer component on the main form of the program periodically looks through the RelayDir folder and calls the TclSmtpRelay component for each message within this folder.

If the message delivery fails, the program moves both the message and it's envelope to the BadDir folder.

// [Delphi]
procedure TForm1.Timer1Timer(Sender: TObject);
   path: string;
   searchRec: TSearchRec;
   envelope: TStrings;
   envelope := TStringList.Create();
      Timer1.Enabled := False;
      path := 'C:\CleverMailBox\RelayQueue\';

      if SysUtils.FindFirst(path + '*.ENV', 0, searchRec) = 0 then
            envelope.LoadFromFile(path + searchRec.Name);
            if (envelope.Count < 2) then Continue;

            clSmtpRelay1.MailFrom := envelope[0];
            ExtractRelayTo(envelope, clSmtpRelay1.MailToList);

            clSmtpRelay1.MailData.LoadFromFile(path + ChangeFileExt(searchRec.Name, '.MSG'));   


            ProcessBounces(path + searchRec.Name);

            DeleteFile(path + searchRec.Name);
            DeleteFile(path + ChangeFileExt(searchRec.Name, '.MSG'));

         until (SysUtils.FindNext(searchRec) <> 0);
      Timer1.Enabled := True;


Run Email Server in Production

To run the Email server in the production environment, the server host must be registered in DNS. There should be at least A and MX records. Otherwise, the server will unable to deliver outgoing Emails, nor accept incoming ones.

Using the SMTP protocol, a relay can transfer mail to another process on the same network or to some other network. With SMTP relay, a mail message may pass through a number of intermediate relay or gateway hosts on its path from sender to recipient. The MX mechanisms of the DNS are used to identify the appropriate next-hop destination for a message being transported. A "relay" SMTP system receives mail from an SMTP client and transmits it, without modification to the message data other than adding trace information, to another SMTP server for further relaying or for delivery. See RFC 2821, RFC 2554, and RFC 974 for more details.


Supported compiler versions

The program can be used in RAD Studio XE3 and higher, including RAD Studio 10.3.3 Rio. If you modify the sources and remove all references to the RAD Studio namespaces in the 'uses' sections, you can use the library in older versions of RAD Studio, CodeGear or Borland Delphi.


The program is distributed under the terms of the GNU Lesser General Public License version 3

Download Email Server in Delphi on GitHub

Clever Internet Suite Download

Learn more about Clever Internet Suite Library

Ask a Question


Sergey Shirokov
Clever Components team

    Copyright © 2000-2021