Home : Network Programming |
Simple Mail Transfer Protocol |
SMTP or Simple Mail Transfer Protocol is a way to transfer email reliably and efficiently. SMTP is a relatively simple, text-based protocol, where one or more recipients of a message are specified (and in most cases verified to exist) and then the message text is transferred. You can think of SMTP as the language that mail servers use to communicate among themselves.
Since this protocol started out as purely ASCII text-based, it did not deal well with binary files. Standards such as MIME (RFC 2045) were developed to encode binary files for transfer through SMTP.
SMTP is used to send mail to the recipient's mailbox. The recipient may use various methods to access the emails in his mailbox. A couple of methods that are very popular are POP3 and IMAP. These protocols allow a user to access his messages stored on a remote mail server.
When the user wants to send a message to someone, the sender-SMTP establishes a two-way transmission channel to a receiver-SMTP. SMTP commands are generated by the sender-SMTP and sent to the receiver-SMTP. SMTP replies are sent from the receiver-SMTP to the sender-SMTP in response to the commands. In case a direct connection does not exist between the sender and the final destination, the message may be sent via one or more relay SMTP-servers. The relay SMTP-servers first act as receivers and then relays the message to the next SMTP. To be able to provide the relay capability the SMTP-server must be supplied with the name of the ultimate destination host as well as the destination mailbox name.
Once the transmission channel is established, the SMTP-sender sends a MAIL command indicating the sender of the mail. If the SMTP-receiver can accept mail it responds with an OK reply. The SMTP-sender then sends a RCPT command identifying a recipient of the mail. If the SMTP-receiver can accept mail for that recipient it responds with an OK reply; if not, it responds with a reply rejecting that recipient (but not the whole mail transaction). The SMTP-sender and SMTP-receiver may negotiate several recipients. When the recipients have been negotiated the SMTP-sender sends the mail data, terminating with a special sequence. If the SMTP-receiver successfully processes the mail data it responds with an OK reply. The dialog is purposely lock-step, one-at-a-time.
Given below are some basic commands with their brief descriptions. The SMTP standard defines many more commands, most of which are optional to implement. For a detailed study, the user should see RFC 821.
HELO <SP> <domain> <CRLF>
Note: <SP> stands for a space and <CRLF> stands for a combination of Carriage Return and Linefeed. The receiver-SMTP identifies itself to the sender-SMTP in the connection greeting reply, and in the response to this command.The first step in the procedure is the MAIL command. The <reverse-path> contains the source mailbox.
MAIL <SP> FROM:<reverse-path> <CRLF>
If accepted, the receiver-SMTP returns a 250 OK reply. The <reverse-path> can contain more than just a mailbox. The <reverse-path> is a reverse source routing list of hosts and source mailbox. The first host in the <reverse-path> should be the host sending this command.
RCPT <SP> TO:<forward-path> <CRLF>
The <forward-path> can contain more than just a mailbox. The <forward-path> is a source routing list of hosts and the destination mailbox. The first host in the <forward-path> should be the host receiving this command.
DATA <CRLF>
If accepted, the receiver-SMTP returns a 354 Intermediate reply and considers all succeeding lines to be the message text. When the end of text is received and stored the SMTP-receiver sends a 250 OK reply.
Since the mail data is sent on the transmission channel the end of the mail data must be indicated so that the command and reply dialog can be resumed. SMTP indicates the end of the mail data by sending a line containing only a period.
The mail data includes the memo header items such as Date, Subject, To, Cc, From etc. To see details about possible memo headers and their formats, read RFC 822 - Standard for the format of ARPA Internet text message
VRFY <SP> <user name> <CRLF>
RSET <CRLF>
NOOP <CRLF>
QUIT <CRLF>
In order to make SMTP workable, the following minimum implementation is required for all receivers:
RCPT
DATA
RSET
NOOP
QUIT
The sender specifies the forward-path of the recepient while sending the mail. This forward-path includes not only the address of the recepient but also the route that is to be taken to reach there. This information is useful if there is not direct link betwen the sender and the recepient. The forward-path may be a source route of the form "@ONE,@TWO:VARUN@THREE", where ONE, TWO, and THREE are hosts. ONE is the name of the host who is receiving this command. ONE will forward this message to host TWO. TWO will in turn forward the message to THREE which is the host on which the user VARUN has a mailbox.
Conceptually the elements of the forward-path are moved to the reverse-path as the message is relayed from one server-SMTP to another. The reverse-path is a reverse source route, (i.e., a source route from the current location of the message to the originator of the message). The reverse-path is used in case a reply has to be sent to the sender.
If a server-SMTP has accepted the task of relaying the mail and later finds that the forward-path is incorrect or that the mail cannot be delivered for whatever reason, then it must construct an "undeliverable mail" notification message and send it to the originator of the undeliverable mail (as indicated by the reverse-path).
When the receiver-SMTP accepts a message either for relaying or for final delivery it inserts at the beginning of the mail data a time stamp line. The time stamp line indicates the identity of the host that sent the message, and the identity of the host that received the message (and is inserting this time stamp), and the date and time the message was received. Relayed messages will have multiple time stamp lines.
When the receiver-SMTP makes the "final delivery" of a message it inserts at the beginning of the mail data a return path line. The return path line preserves the information in the <reverse-path> from the MAIL command. Here, final delivery means the message leaves the SMTP world.
The preceding two paragraphs imply that the final mail data will begin with a return path line, followed by one or more time stamp lines. These lines will be followed by the mail data header and body. A sample header from a mail I sent to myself is listed below.
From malhotra_g@vsnl.net Fri Nov 7 19:17:53 2003
Return-Path: <malhotra_g@vsnl.net>
Delivered-To: gaurav@deimos.csa.iisc.ernet.in
Received: from csa.iisc.ernet.in (csa.iisc.ernet.in [144.16.67.8])
by deimos.csa.iisc.ernet.in (Postfix) with ESMTP id 8EF794E662
for <gaurav@deimos.csa.iisc.ernet.in> ; Fri, 7 Nov 2003 19:17:53 +0530 (IST)
Received: by csa.iisc.ernet.in (Postfix)
id 30AA52BDF9; Fri, 7 Nov 2003 18:57:32 +0530 (IST)
Delivered-To: gaurav@csa.iisc.ernet.in
Received: from smtp2.vsnl.net (smtp2.vsnl.net [203.200.235.232])
by csa.iisc.ernet.in (Postfix) with ESMTP id D45FA2BC9F
for <gaurav@csa.iisc.ernet.in> ; Fri, 7 Nov 2003 18:57:31 +0530 (IST)
Received: from vsnl.net ([127.0.0.1])
by smtp2.vsnl.net (iPlanet Messaging Server 5.2 HotFix 1.16 (built May 14
2003)) with ESMTP id <0HNZ00HE9I8UA7@smtp2.vsnl.net> for
gaurav@csa.iisc.ernet.in; Fri, 07 Nov 2003 19:16:06 +0530 (IST)
Received: from ([172.16.28.141])
by smtp2.vsnl.net (InterScan E-Mail VirusWall Unix); Fri,
07 Nov 2003 19:16:06 +0530 (IST)
Received: from [172.16.28.182] by pop2.vsnl.net (mshttpd); Fri,
07 Nov 2003 18:46:06 +0500
I wrote this mail on the webmail interface provided by vsnl to its subscribers. Starting from the bottom, we can see the hosts that were traversed, before this mail was delivered to my mailbox on csa.iisc.ernet.in.
Since this mail was written using webmail, the contents were received by the http server on vsnl. The http server forwarded this mail to a Virus Scanner which forwarded it to smtp2.vsnl.net. The mail was then sent to csa.iisc.ernet.in which is the mail exchanger for the csa.iisc.ernet.in domain. The mail has technically been delivered to me but since my mailbox physically resides on deimos.csa.iisc.ernet.in, csa.iisc.ernet.in internally forwards it to host deimos. Once the mail has reached deimos, I can open my mailreader and view the mail.
Suppose you have an email ID, how do you find out the address of the smtp server to which mail for that ID is supposed to be sent? The answer to this is provided by DNS. MX records in the DNS database store the Mail eXchangers for a domain. A mail exchanger is the host name of a smtp server that is a gateway for all mails entering a domain. In other words, it is a server that takes care of all mails sent to addresses within that domain. When an smtp server needs to send an email to a address, say abc@xyz.com it queries the DNS for the mail exchangers for xyz.com. The sender can then connect to the mail exchanger and deliver the message. It is possible for a single domain to have multiple mail exchangers for reliability.
Querying the DNS for Mail Exchangers for hotmail.com yielded the following results:
mx2.hotmail.com
mx3.hotmail.com
mx4.hotmail.com
mx1.hotmail.com
Note that mail exchangers will typically only accept mails addressed to users within that domain. SMTP servers provided by your ISP or local system administrator will accept mails for any destination and then relay it to the appropriate mail exchanger.
If you are in CSA at IISc, you may want to learn how to use telnet from within the Computing Lab.
In this section I will show you a sample interaction I had while sending mail (to myself :)) using telnet. Note that SMTP servers listen to port number 25. The lines entered by me are prefixed by a "C:" and the server's replies are prefixed with a "S:". This is the approximate sequence of commands that would be used by a server sending mail to another.
C: ]$ telnet mx1.vsnl.net 25
Trying 203.200.235.229 ...
Connected to mx1.vsnl.net.
Escape character is '^]'.
S: 220 mx1.vsnl.net -- Server ESMTP (iPlanet Messaging Server 5.2 HotFix 1.16 (built May 14 2003))
C: HELO csa.iisc.ernet.in
S: 250 mx1.vsnl.net OK, [144.16.67.10].
C: VRFY malhotra_g
S: 252 2.5.0 Possible local address <malhotra_g@mx1.vsnl.net>
C: MAIL FROM:<gaurav@csa.iisc.ernet.in>
S: 250 2.5.0 Address Ok.
C: RCPT TO:<malhotra_g@vsnl.net>
S: 250 2.1.5 malhotra_g@vsnl.net OK.
C: DATA
S: 354 Enter mail, end with a single ".".
C: Subject: test
C: This is a test.
C: .
S: 250 2.5.0 Ok.
C: QUIT
S: 221 2.3.0 Bye received. Goodbye.
Connection closed by foreign host.
Want to learn more?
Back to Network Programming |