Monday, May 3, 2004

SMTP: Simple Mail Transfer Protocol


Recently I had to write a simple SMTP client module for an application for strange reasons :P. Eventhough you will never have to write an SMTP module ground up since there are many libraries that give you this functionlaity, you might like to know what actually happens when you try to send a mail. Leave a comment if you are one of them. I would like to know if anyone would read blogs like these :).



SMTP is defined in RFC 821. What we currently use is the SMTP with some extentions (authorization, SSL, etc.) as specified in RFC 2821. The new specification also makes sure that servers with the old SMTP implementation also works.



Let us start with the simpler RFC 821.

Basically the SMTP client (your SMTP program/ mail client) sends a set of commands and data to an SMTP server that actually sends it. As you would already know, to send commands means to open a connection to the SMTP server and write the command strings/ text/ bytes to the socket stream.


First a connection is established by opening a socket to the server. For this we use the server host name and the port to connect to. The default port for SMTP is 25. Inorder to see what happens try to open a telnet connection to an SMTP server on port 25. You can try the commands listed below to actually see what happens and send a mail from a telnet client.



The first command to the SMTP server is


HELO<SP><put-your-domain-name-here><CRLF>


It means HELO followed by a space followed by your machine name (could be anything) and a Carriage return + Line feed (Press enter). for eg.
HELO mullassery.com

You should see a response starting with 250 which means that it was successfully received. Any number starting with 400+ or 500+ indicates and error. Continue if it was successful.



Now try the next command to specify a from address.


MAIL<SP>FROM:<put-your-from-address-here><CRLF>


Note that you can actually fake a FROM address by just providing any email address as the argument of this command!



Then specify the recipient address. Type


RCPT<SP>TO:<put-your-to-address-here><CRLF>


Since this is a trial/ test, it is better to send it to yourself, so that you can check if you actually got it.



Now its time to add the actual mail body. Type


DATA<CRLF>


This command starts a "session" to input/ type-in the mail body. This time you will get a response starting with 300 (354). Numbers starting with 300+ indicates a command partially completed. In this case the it is considered completed only when the actual body is also entered.



No enter the mail body text. Start with a a mail header

From:put-your-from-address

To:put-your-to-address

Subject:put-your-subject-line

Yes, you have to repeat these details. This is what the email client shows when you open the mail. Now start typing in the actual text...


When you have completed entering the text stop by adding a

<CRLF>.<CRLF>

You have to press enter type a dot and press enter again. This indicates the end of the message entry. This dot will not be included in the actual message body.



Now you can ask the server to send the mail. Type


SEND<CRLF>


In most servers this command will add the mail to the current queue of mails to be sent. To send another mail you can start with MAIL FROM: again. When you want to quit, type


QUIT<CRLF>


Now open your email client and see if you got it. You should have received it in a few minutes based on how often the SMTP server sends it mails.



This covers the basic subset specified in RFC 821. I will add the details of other commands and the extentions in another blog.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home