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:
HELO / EHLO This command is used to identify the sender (client) to the SMTP server.
MAIL FROM: Specifies the e-mail address of the sender.
RCPT TO: Specifies the e-mail address of the recipient.
DATA Starts the transfer of the actual data (body text, attachments etc).
RSET (RESET) Specifies that the current mail transaction will be aborted.
VRFY (VERIFY) Asks the receiver to confirm that the argument identifies a user or mailbox.
HELP This command causes the server to send helpful information to the client.
QUIT Quits the session.
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 mail@samlogic.com to john@mail.com in the following example:

C: HELO client.mydomain.com
S: 250 Hello client.mydomain.com
C: MAIL FROM:<mail@samlogic.com>
S: 250 OK
C: RCPT TO:<john@mail.com>
S: 250 OK
C: <The message data (body text, subject, e-mail header, attachments etc) is sent>
C: .
S: 250 OK, message accepted for delivery: queued as 12345
S: 221 Bye
(C=Client, S=Server)

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

RFC 5321 - Simple Mail Transfer Protocol
RFC 1869 - SMTP Service Extensions
Do you want to read more articles and tips?
If you want to read more articles and tips about SMTP servers and related topics you can follow us on Facebook or Twitter, or subscribe on our newsletter. You can also read our blog.
Related products:
SamLogic MultiMailer
SamLogic Internet Components

Other articles
More articles are available from the article index page.


Related Articles


Article written by: Mika Larramo