What is a Mailserver Good for?

Hmmm... Good question... My internet provider obviously has got a mailserver (or I wouldn't be able to send and receive mail) , all the people's internet providers that send mail to me obviously have got one either and that f*³@€ bunch of spammers dropping me their unwanted ads must have got one as well, so why should I have one?

To tell you the truth, you do not need one.
But if you've got one running, it can make some things easier for you.

Another point to have an own mailserver for is: you (yes, right! Just you!) are obviously someone who is interested in programming. Let it be your job or just your passion, but you are interested in (or you wouldn't have read that far). Are you tired of programming 'Hello world's or Eratosthenes sieves? Databases are not frightening you anymore? Stumbled over Indy and want to know what you can do with it? Fine, you'll see!

Now, if you've got a mailserver, what can you do with it?

I'll give you an idea:

I must admit, due to my strong relations to computers, my home may look a bit over-computerized compared to the 'normal' user's home: Three in my (small) office, one in the living-room, one for my wife and another one for the twins, all connected in a LAN.
So what if I'd like to see in the living-room whether there has arrived some mail for my office? Speaking in terms of 'Microsoft Outlook' (regardless of express or not) that would turn out to be something rather difficult, especially when it comes to the answering of mail and (probably later, when the weekend is over) printing and archiving the correspondence to the appropriate customer's file.
This is a scenario where an own mailserver drops in: Hail Super(post)man! It can frequently check all the mailboxes (every member of the family runs an own account) and collect the letters, check for virus/trojans/worms and other malware (rather: have the mail checked, I do not want to write malware-scanners!) and can start to distribute the incoming mail into the mailboxes: one for me, one for the twins, 34 for spam....
Now all the mails are stored within my very own network and with the help of a mailclient I could access any mail from any computer.

So much for the fiction, now let's get it divided into functional parts  

What do we need to do that job?
Well, at first we need some places to store each individual’s mails in, separated from each other ones. The word for this is 'Mailbox' and the simplest data-structure to realize its functionality would be a 'Folder' or 'Directory' whatever name you prefer.
Mail we are going to receive and send will be processed by other programs we do not know of. So we should save our mail in an independent format. I will name this format 'Raw' and let it represent an individual mail that could be received by ANY mail-program that behaves like a normal mail-program does.
When it comes to looking at the received mails, we need a program that shows us the contents on the screen, or rather: this is the very minimum a program of this category should perform. It is named 'Mail Client' as opposite to our 'Server'. Surely, you've got one, so we don't need yet to write one too.

We need the functionality to communicate with our Mail-Client on one side and to communicate with our provider's Mail-Server on the other side to exchange the mails.
Fortunally Indy drops in here providing us with the necessary functionality:
Indy has got components we will use here for
•	POP3 – Mailservers	(TIdPop3Server)
•	POP3 – Mailclients	(TIdPOP3Client)
•	SMTP – Servers	(TIdSMTPServer)
•	SMTP – Clients 	(TIdSmtpClient)

Mail is sent from a client to a server with SMTP (Simple Mail Transport Protocol) and is received with POP3 (Post Office Protocol number 3) which are two widely used standard although there are enhancements which can do better some of the required jobs. Indy has got components for those too, but at first we will stick to the easier ones.
The above Indy-components will make the job to be done much easier, so we could put aside the (very complicated) part of communicating and -as you will see in the examples- we just have to put into the right places our needs for the execution of some simple commands.

We need to save information concerning the mailboxes at our provider (like account names and passwords) and some information for the mailboxes on our local mail-server (account names to identify the family member, passwords and the folder name to store the mails in). This could be done with a simple .ini – file or with something as complicated as a small database. I'll leave this for your own taste, in this example I'll just cover Indy, not databases.
Well, that's all we need, so you we can start right off. The example covers the pure basics of a mail-server so far and it will help you to implement whatever you want to do. Feel free to use it (at your own risk, as usual) for private or commercial use. Do not forget to fulfil the Indy licence agreement, which is really easy to be done.

Happy coding
Jörg Meier  (Bob)
emil@jmeiersoftware.de