The Mechanics of SMTP

The SMTP protocol requires only that a host be able to send straight ASCII text to another host. Typically, this is done by contacting the SMTP port (port 25) on a mail server. You can do this using the Telnet program. For example:

[[email protected] /]# telnet mailserver 25

where the host mailserver is the recipient's mail server. As you may be aware, the Telnet program's main purpose is to log in remotely to other machines. There are a number of reasons that it's no longer a good choice for doing that, some of which are covered in Module 14, but the biggest one is also what makes Telnet a good debugging tool for other protocols. That is, Telnet sends plain ASCII text to a network port. The "25" that follows mailserver in the preceding example tells Telnet that you want to communicate with the server's port 25 rather than the normal port 23. (Port 23 is used for remote logins, and port 25 is for the SMTP server.) The mail server will respond with a greeting message such as this:

22 0 mail.example.com ESMTP Postfix

You are now communicating directly with the SMTP server.

Although there are many SMTP commands, the four worth noting are

The helo command is used when a client introduces itself to the server. The parameter to helo is the host name that is originating the connection. Of course, most mail servers take this information with a grain of salt and double-check it themselves. For example:

helo super-duper-strong-coffee.com

If you aren't coming from the super-duper-strong-coffee.com domain, many mail servers will respond by telling you that they know your real IP address, but they will not stop the connection from continuing. (Some mail servers include a comment about why you didn't use a truthful helo statement.)

The mail from: command requires the parameter of the sender's e-mail address. This tells the mail server the e-mail's origin. For example, mail from: [email protected]

means the message is from [email protected].

The rcpt to: command also requires the parameter of an e-mail address. This e-mail address is of the recipient of the e-mail. For example, rcpt to: [email protected]

means the message is destined to [email protected].

Now that the server knows who the sender and recipient are, it needs to know what message to send—that is done by using the data command. Once issued, the server will expect the entire message with relevant header information, followed by a period on a line by itself (technically, a <CR><LF>.<CR><LF> sequence, for those that are curious about such details). Continuing the example, [email protected] might want to send the following message to [email protected]:

3 54 End data with <CR><LF>.<CR><LF> From: Dilbert <[email protected]>

To: Pointy Hair Manager <[email protected]>

Subject: On time and within budget.

Just an fyi, boss. The project is not only on time, but it is within budget too!

250 queued as 26F7B1D01A2

And that's all there is to it. To close the connection, enter the quit command.

This is the basic technique used by applications that send mail—except, of course, they usually use C code rather than Telnet, but the actual content sent between the client and server remains the same.

Now that we've shown you how SMTP works, we're going to walk through a first-hand example. This project assumes that you have a working mail server (not necessarily on your own machine) that you can reach. You'll need to fill in your own correct information in the places indicated.

1. First, telnet to port 25 on the mail server where you normally receive mail. Replace your own server name in the following example.

telnet mailserver.my-domain-name.com 2 5

data

2. Next, type helo followed by the name of the server you're pretending to be. Using the host name of the machine you're working on is probably not a bad idea. Whether any checking is done on this value will depend entirely on the paranoia of the server you're connecting to. If you use the name of the machine you're connecting from, you shouldn't have any problems.

helo my-hostname.my-domain-name.com

3. Enter the mail from: now. Remember from the previous section that this identifies the sender of the message. If you're dealing with a properly paranoid mail server, this value should at least be a valid e-mail address. You can use your own e-mail address here.

mail from: [email protected]

4. Now enter the e-mail address of the recipient, after the rcpt to: command. This will again be your own e-mail address for this test.

rcpt to: [email protected]

5. Next comes the data command:

data

6. Finally we get to the meat of the e-mail. If you want this message to actually be readable with standard mail software, you'll need to have proper headers. Start with entering the simple headers shown here:

From: [email protected] To: [email protected] Subject: Project 13-1

7. Enter a blank line to separate the headers from the body of the message.

8. Type any message body that you like; then enter a period on a line by itself to end the message.

This is really cool. I can test my mail servers just like this.

8. Type quit to close the connection.

9. Fire up your favorite mail reader and see if your message arrived.

Was this article helpful?

0 0

Post a comment