Application Layer (email services) – Simple Mail Transfer Protocol (SMTP Server Gateway )

Howdy fellas !!

If you are a regular reader of my earlier posts, by now you might already be versed with the working of a basic email application that we use in our day to day life. In the previous post, we have seen the User-agent (MIME) in detail. Now its time to understand how an actual mail transfer agent really works. 

The actual mail transfer is done through message transfer agents(MTA). In the internet the source machine establishes a connection to port 25 of the destination machine so as to deliver an email. To send any mail, a system must have the client MTA, and to receive mail, a system must have a server MTA.

The formal protocol that specifically defines the MTA client and server on the Internet is called the Simple Mail Transfer Protocol (SMTP).


Application Layer (email services) - Simple Mail Transfer Protocol (SMTP Server Gateway )


Basically, we can say that the two most important functions provided by the message transfer agent(SMTP agent) is :

  • It will first accept the incoming connections and copy messages from the sender to the receiver’s mailbox.
  • Return an error message to the sender if any message is not delivered.

SMTP is used two times(refer pic above), between the sender and the sender’s mail server, and between the two mail servers.

Further we will see in the next post, another protocol is needed between the mail server and the receiver.Now let us understand the functioning of SMTP protocol in detail(refer the picture above to relate all the following steps):

Initially, a TCP connection between sender and port 25 of the receiver is established, the sending machine will act as a client and the receiving machine will act as a server.

Now, the client will wait for the server to take action (initiative) in communication.

The server now sends a line of text which reveal its identity and declares its willingness/unwillingness to receive the mail. If the server is not yet prepared then the client will release the connection, and wait for some time and try again after sometime.

So , if the server is willing to accept email , then the client will announce the sender of the email and its recipient.

If the recipient (address) is present in the destination, then the server will ask the client to start sending the message(a stream of bytes).  The client then sends the message and the server send back its acknowledgment.

As TCP provides a reliable byte stream , no checksum is required. All the emails will be sent now.

After exchanging all the emails , then they release the connection.

SMTP uses numerical codes and is a simple ASCII protocol.

Components of Email System:

The three main components of the internet mail system are :

  • User-Agents
  • SMTP sender
  • SMTP reciever

User-agent will help in framing any message for the user (by accepting the input fields as seen in previous post) . Each consists of the header field , recipient’s mail address, and other info.

All the messages will be lined up in a queue and provide input to an SMTP sender program.Then the SMTP sender will take the message from the queue and transmit it to a proper destination host via SMTP connection through TCP connection to port 25.

The SMTP protocol is used to transfer a message from SMTP sender to SMTP receiver (with the help of a TCP connection) . The SMTP receiver accepts each arriving message and stores it in the user mailbox .

If the mail is to be forwarded , then the SMTP receiver copies it to the outgoing mail queue.

SMTP Commands

SMTP uses commands and responses to transfer messages between a MTA client and an MTA server . Commands are sent from the client to the server(keyword used) and responses are sent from the server to the client(three digit code).

Application Layer (email services) - Simple Mail Transfer Protocol (SMTP Server Gateway )


After establishing a TCP connection, SMTP sender sends commands over the connection to the receiver.Each command consists of a single line of text which begins with 4 letter command code.Refer below table for list of all the commands :

Name Description
HELO Send identification of sender
MAIL Identifies originator of mail
RCPT Identifies recipient of mail
DATA Transfer message text
RSET Abort current mail transaction
NOOP No operation
QUIT Close TCP connection
SEND Send mail to terminal
SOML Send mail to terminal if possible , else to mailbox
SAML Send mail to terminal and mailbox
VRFY Confirm username
EXPN Return membership of mailing list
HELP Send system-specific documentation
TURN Reverse role of sender and receiver

Now let us see some of the response codes(sent by the server) :

Code Description
211 System status or help reply
214 Help message
220 Service ready
221 Service closing transmission channel
250 Request command completed
251 User not local
421 Service not available
450 Mailbox not available
500 Syntax error; unrecognized command
501 Syntax error in parameters or arguments
502 Command not implemented
503 Bad sequence of commands

SMTP Operation (Mail Transfer Phases)

The process of transferring a particular mail message occurs in three phases:

  • Connection establishment (set up)
  • Mail transfer (exchange of command and responses)
  • Connection termination

Connection Setup 

The sender initiates a TCP connection with the receiver.Once connection established, then the receiver identifies itself with ” 220 Service Ready “. Then the sender identifies itself with HELO command. Now the receiver accepts the sender’s identification with “250 OK” . 

Mail Transfer 

Now the SMTP sender may send one or more messages to SMTP receiver (since the connection is already established).It includes three steps :

  • A MAIL command initially identifies the originator of the message.
  • One or more RCPT commands identify the recipient for this message.
  • A DATA command transfer the message text.

Connection Termination

It includes two steps:

  • First the sender sends a QUIT command and wait for a reply.
  • Second step is to initiate a TCP close operation for the specific TCP connection. The receiver then initiates its TCP close after sending its reply to the QUIT command.

Email Gateways:

An email messaging system (using SMTP protocol) will work if both the sender and receiver are connected to the internet and can support TCP connection between them.

But practically there are many machines which may not be connected to the internet and still want to send and receive messages.

This can be made possible by the use of application layer email gateways. A gateway process is supposed to extract the incoming messages from one queue and deposit them in the other.

Last but not the least, you may be interested in the last part of the email application i.e the message access agent (POP and IMAP).So stay tuned for the next post..

Spread the Wisdom !!

Techie Aric

Aric is a tech enthusiast , who love to write about the tech related products and 'How To' blogs . IT Engineer by profession , right now working in the Automation field in a Software product company . The other hobbies includes singing , trekking and writing blogs .

Leave a Reply

Your email address will not be published. Required fields are marked *