|
Submitted 20 September 2006
This article demonstrates how to implement multi-session Mail Server in Delphi using server 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.
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.
clSmtpServer1.Port :=25; clSmtpFileHandler1.Server := clSmtpServer1; clSmtpFileHandler1.MailBoxDir := 'C:\ CleverMailBox; clSmtpFileHandler1.RelayDir := 'C:\CleverMailBox\RelayQueue'; clSmtpServer1.Start; ... |
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.
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; |
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:
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; |
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.
Please use the link below to download the full source code for the Mail Server program:
- CleverMailServer.zip - Mail Server Sources
- clLogger.zip - Clever Components Logger Sources
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 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
|