The SMTP Protocol / The SMTP Server
When you send an e-mail message from your computer to another computer it will go trough many different servers before it actually reaches your recipient's computer. The first stop for your e-mail message is an SMTP e-mail server. This server communicates directly with your computer using a protocol named SMTP. SMTP is an abbreviation of Simple Mail Transfer Protocol and it is the de facto standard for e-mail transmission from clients to mail servers via the Internet. SMTP is an old protocol and was used already in the 1980s to transfer messages between computers.
The SMTP Protocol
Client computers communicate with e-mail servers (SMTP servers) using the SMTP protocol. SMTP is a relatively simple, text-based protocol, that use a limited set of commands and reply codes for its communication. The SMTP protocol is defined in RFC 2821 - Simple Mail Transfer Protocol. The objective of Simple Mail Transfer Protocol is to transfer e-mail messages reliably, efficiently and easily. SMTP is independent of the particular transmission subsystem and requires only a reliable data connection.
SMTP uses Port 25
SMTP servers normally communicate with the outside world trough the TCP port 25. It means that a client computer that wants to connect to an SMTP server must establish a connection to the server port number 25 and use TCP (Transmission Control Protocol) to its low-level communication. However, nowadays some mail servers can also use another ports for SMTP communication, for example port 2525.
The SMTP Commands
The SMTP protocol consists, in its basic form, of about ten commands that is used for the communication between the client and the server. Below are some of the commands used in an SMTP communication described:
The client sends these commands to the mail server and the server replies with numerical codes and additional text messages after the numerical code. These reply codes tell the client if the previous sent command succeeded or failed.
Commands and replies are composed of characters from the ASCII character set. Every command is normally 4 characters wide (sometimes with an additional sub command after) and the reply code always consists of 3 digits. The length of the text after the reply code can vary depending of which mail server used, but the text length is normally less than 100 characters (but can be up to 508 characters according to the SMTP protocol definition). A space character is used to separate the numerical code from the text message.
As you can se in the table above there are two commands used to identify the sender: HELO and EHLO. The HELO command is an older one and can be used if only basic functionality is required. If more advanced functionality is required the client should use the EHLO command instead. EHLO is a newer one and puts the server in an extended mode and more advanced commands can be used. Most SMTP servers today support the EHLO command.
If you want to have more information and a deeper understanding of the SMTP commands you can read the following tecnical article: SMTP Commands Reference.
Example of an SMTP Communication Session
Below you find an example of a basic communication between a client and an SMTP server. An e-mail message is sent from firstname.lastname@example.org to email@example.com in the following example:
The HELO command is used to identify you to the SMTP server. If everything is ok the SMTP server will respond with a 250 reply code.
The MAIL FROM: command tells the SMTP server that an e-mail message will be sent. It also informs the server the e-mail address of the sender. If the mail server approves the sender's e-mail address the next command to send is the RCPT TO: command. This command tells the mail server the e-mail address of the recipient.
If we have succeeded this far, it is time to send the actual data. This is done by first sending the DATA command and then the actual data after we have received a 354 reply code from the server. To tell the server when there is no more data left we must send a line containing a single dot ("<CR><LF>.<CR><LF>").
If the mail server have accepted the e-mail message for delivery it will respond with a 250 reply code. Then you can quit the session by sending the QUIT command. The server will then respond with a 221 reply code and your e-mail message is on its way!
The E-mail Message's Way from Client to Client
After you have sent the e-mail message to the SMTP server that communicates with your computer the message still has a long way to travel before it reaches your recipient's computer. The moment after the e-mail message has been successfully transferred to your SMTP server; the server must find your recipient's mail server and transfer the message to that server. To locate the server that the recipient uses, the recipient's e-mail address will be examined and the domain name extracted by reading the information after the "@" character (the domain name is always found after the "@" character in the e-mail address). By using this domain name and then checking with a Domain Name Server (DNS), the location (the IP number) of this server will be found. After that, a communication line will be established between your mail server and your recipient's mail server.
The communication between these two mail servers are basically the same as the communication between your computer and your SMTP server. The SMTP protocol is used and the port number 25 (at your recipient's mail server) is used. The commands used are normally also the same and the e-mail message is transferred in similar way as when it was transferred from your computer to your SMTP server.
However, sometimes there can be technical problems at the recipient's server and the e-mail message can not be transferred. To handle such a situation the message goes into a queue. The SMTP server on most machines uses a program called sendmail to do the actual sending, so this queue is called the sendmail queue. Sendmail will periodically try to resend the messages in its queue. For example, it might retry every 15 minutes. After 4 hours, it will usually send you a piece of mail that tells you there is some sort of problem. After 5 days, most sendmail configurations give up and return the mail to you undelivered.
If the message have been successfully transferred to your recipient's mail server, it will then be stored in his/her POP3 server and when the recipient start downloading his/her e-mails, your e-mail message will appear in the inbox of the recipient's e-mail client program.
RFC 5321 - Simple Mail Transfer Protocol
RFC 1869 - SMTP Service Extensions
SamLogic Internet Components
More articles are available from the article index page.
|Article written by: Mika Larramo|