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      

Complete Mail Server in Delphi, implementing POP, SMTP, IMAP, and Relay

Updated on November 25, 2019
Submitted on September 20, 2006

Update Notes

The updated version of the Mail Server program is described in the Email Server in Delphi with POP3, SMTP, and IMAP Support article.
This updated program utilizes the Clever Internet Suite version 9.4 and provides a user interface for configuring the server. The sources, which use previous versions of Clever Internet Suite are included, as well.

The sources are available on GitHub

Introduction

This article demonstrates how to implement a multi-session E-mail Server in Delphi using server Internet components from the Clever Internet Suite library.

The provided project represents a fully functional multithreaded Mail Server, which accepts POP3 and IMAP connections, delivers E-mails via the SMTP protocol and relays outgoing E-mails to end-recipients across the Web.

IMAP, POP3 and SMTP services

The Clever Internet Suite includes all components, which do all the work. No extra libraries are required. The following server components are needed: POP3 Server, IMAP Server and SMTP Server. All these components work with user data (messages and mailboxes) in-memory using special component events. You can handle these events and store mailboxes and their messages to, e.g., a database or the disk.

The Clever Internet Suite includes the following three components, which automatically handle all server component events and allow to store user data on the disk like MDaemon, Communigate, and other mail servers: POP3FileHandler, IMAPFileHandler and SMTPFileHandler.

Simple link each File Handler component with a corresponding server component and specify the Root Folder to store the user data: Emails, mailboxes, messages.

clSmtpServer1.Port :=25;

clSmtpFileHandler1.Server := clSmtpServer1;



clSmtpFileHandler1.MailBoxDir := 'C:\ CleverMailBox;

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



clSmtpServer1.Start;

...

User Accounts

Iet's create user accounts and share them between POP3, IMAP, and SMTP server components. This allows us to use the same mailboxes in POP3 and IMAP sessions, send E-mails to local recipients, and accept incoming messages from external senders.

clImap4Server1.UserAccounts.Clear();

cnt := Ini.ReadInteger('USERS', 'Count', 0);

for i := 0 to cnt - 1 do

begin

   account := clImap4Server1.UserAccounts.Add;



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

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

end;

Relay Messages

There are two kinds of message recipients: local users, which belong to this server, and external users out of the server.

When a user sends a message to a local recipient, the message is accepted by the SMTP server. The SMTP server saves received messages locally to a user's mailbox folder.

When accepting a message to an external recipient, the SMTP server saves it into a special Relay Folder. You can check out this Relay Folder and deliver all outgoing E-mails to end-recipients. The SMTP Relay component does all the work:

procedure TForm1.Timer1Timer(Sender: TObject);

var searchRec: TSearchRec;

begin

   if FindFirst(relayQueueFolder + '*.*', 0, searchRec) = 0 then

   begin

      repeat

         clSmtpRelay1.MailData.LoadFromFile(relayQueueFolder + searchRec.Name);

         clMailMessage1.HeaderSource := clSmtpRelay1.MailData;



         clSmtpRelay1.MailFrom := clMailMessage1.From;

         clSmtpRelay1.MailToList.Text := ExtractRelayTo(clSmtpRelay1.MailData);

         clSmtpRelay1.Send();



         DeleteFile(relayQueueFolder + searchRec.Name);

      until FindNext(searchRec) <> 0;

      FindClose(searchRec);

   end;

end;

This code utilizes the TTimer VCL component to periodically look through the Relay Queue and run the message delivering process. The SMTP Relay component allows us to easily monitor the delivery status and detect non-delivered E-mails.

Download Source Code

Please use the link below to download a full source code for the Mail Server program:

Download on GitHub

The Mail Server program uses Ini files for saving all server and user account information. This program writes a detailed log about all aspects of IMAP, POP3, and SMTP sessions.

The code is constantly refined and improved. 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-2024