Complete Mail Server in Delphi implementing POP, SMTP, IMAP and Relay
Submitted on September 20, 2006
This article demonstrates how to implement multi-session Mail Server in Delphi using server Internet components from Clever Internet Suite
With this simple program you can use your PC as fully functional multithreaded Mail Server which can Access POP3 and IMAP mailboxes, Deliver Emails via SMTP protocol and Relay outgoing Emails to recipients across the Web.
IMAP, POP3 and SMTP services
The Clever Internet Suite provides all components to do all the work. No extra libraries required. For accepting user connections the following server components are used: POP3 Server, IMAP Server and SMTP Server. All these components work with user data (messages and mailboxes) in-memory by using different component events. You can handle these events and store mailboxes and its messages manually to, e.g., database. In this article I would like to outline simple yet elegant way.
The Clever Internet Suite includes the following three components which automatically handle all server component events and allow storing user data on the disk like MDaemon, Communigate and other mail servers: POP3FileHandler, IMAPFileHandler and SMTPFileHandler.
Simple bind each File Handler component with the corresponding server component and specify the Root Folder to store the user data: Emails, mailboxes, messages to Relay.
For allowing users to connect to the Mail Server, let's create user accounts and share them between POP3, IMAP and SMTP server components. This will allow you to use the same mailboxes in POP3 and IMAP sessions, send Emails to local recipients and accept incoming messages from another users.
There are two kinds of message recipients: local users which are on the same server and external users out of the server.
When user sends a message to the local recipient, the users' Email Client submits this message to the SMTP Server. SMTP Server saves received message locally within the corresponding user mailbox folder.
When accepting an external message, SMTP Server stores it into a special Relay Folder. You can check this Relay Folder and deliver all outgoing Emails to end recipients. The SMTP Relay component will do all this work:
In this code I have used the TTimer VCL component to check the Relay Queue and run the message sending process. With SMTP Relay component you can easily monitor the message relaying status and detect non-delivered Emails.
Download Source Code
Please use the link below to download the full source code for the Mail Server program:
The Mail Server program uses Ini files for storing all server and user account settings. This program writes the detailed log about all aspects of IMAP, POP3 and SMTP sessions.
The 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.