SMTP Described
Simple Mail Transfer Protocol is the Internet standard for the transmission of email messages from sender to the receiver mail server. You may recall from the Email Beginnings that this transmission is accomplished through the use of mail transfer agents (MTAs) located on the mail server and user agents (UAs) located on the user's local machine. The job of the MTA is to send and receive mail from other MTA's. The job of the UA is to interface with the MTA and to provide a user friendly environment for the end user.
The SMTP protocol essentially provides a means to transfer messages from mail server to mail server. A server receiving such a transfer may either be the final destination or simply a relay point for the message. Receiving email is accomplished with either of the protocols POP or IMAP. (More information about these protocols can be found in the POP3 Overview topic or the IMAP4 Overview topic). The SMTP transfer is a three step process: authenticating the sender, sending mail, and closing the connection. These processes ensue "lock step", meaning that the sender sends a command which the recipient processes and responds to.
The examples that follow show the SMTP commands that occur between two SMTP mail transfer agents. When using PowerTCP Mail for .NET, all authentication/logging in is automatically handled by the Smtp component.
SMTP Authentication Described
Prior to authentication, the receiver will (normally) be listening on port 25 for TCP connections. Once a connection is made, the sender may issue the HELO command, followed by the sender's domain name. The recipient will in turn identify itself to the sender. The process may look something like this.
Recipient < wait for connection on port 25 > Sender: < make TCP connection > Sender: HELO sender.com Recipient: 250 server1.dart.com
An alternative authentication method is specified using the EHLO command. This command is functionally equivalent to the HELO command except it expresses the desire of the sender to use service extensions (these are a way to enhance SMTP and are covered here). Authentication using the EHLO command may look like this.
Recipient < wait for connection on port 25 > Sender: < make TCP connection > Sender: EHLO sender.com Recipient: 250 server1.dart.com 250-HELP 250-EXPN 250-XREMOTEQUEUE 250-ETRN 250-PIPELINING 250-SIZE
In this example the response from the server is the same as with the HELO command except it announces the service extensions it supports. At this point the authentication stage is complete, and the sending mail process can continue. With pipe-lining the entire above process takes only one send and receive.
SMTP Sending Mail Described
Once the sender has been authenticated, the next step is to send the mail message. This is a simple process involving three commands, MAIL FROM, RCPT TO, and DATA. The process begins with the MAIL FROM command followed by the sender's address, which specifies the sender of the mail. If the recipient can accept mail, it responds positively with 250 OK. The sender then sends a series of RCPT TO command, each followed by the address of a mail recipient. The receiver will either accept or reject each mail recipient. If a recipient is rejected, the mail transaction still continues. The sender will then use the DATA command to send the actual message. The server should respond to this with a 354 Intermediate reply and will consider all succeeding lines to be the message. This message includes header fields with labels such as Date, Subject, To, Cc, and From followed by the message body. When all data has been sent, the sender issues a <CRLF>.<CRLF>, letting the recipient know all data has been transmitted. The DATA command should fail only if the mail transaction was incomplete (for example, no recipients), or if resources are not available on the server. The process of sending mail may look like this:
Sender: MAIL FROM: <Smith@Alpha.ARPA> Recipient: 250 OK Sender: RCPT TO: <Jones@Beta.ARPA> Recipient: 250 OK Sender: RCPT TO: <Green@Beta.ARPA> Recipient: 550 No such user here Sender: RCPT TO: <Brown@Beta.ARPA> Recipient: 250 OK Sender: DATA Recipient: 354 Start mail input; end with <CRLF> . <CRLF> Sender: Blah blah blah... Sender: ...etc. etc. etc. Sender: <CRLF>.<CRLF> Recpient: 250 OK
Once this stage is complete, the mail has been sent and the connection must be closed. The mail exchange above is accomplished using PowerTCP for .NET by using the Send method.
SMTP Closing the Connection Described
Now the mail has been sent and all that remains is to close the connection. The sender accomplishes this by using the QUIT command. The recipient replies with 221 verifying that the connection is closed. This transaction may look like this:
Sender: QUIT Recipient: 221 closing channel < TCP connection ended >
Once this command has been given, the sending mail process is complete. The example above is automatically accomplished when using PowerTCP Mail for .NET.
By default, commands and replies are composed of characters from the US-ASCII (7-bit) character set. If the server supports the extended protocol for an 8-bit byte (octet) transmission channel, it responds with this extension at greeting time if the EHLO command is used. PowerTCP supports 8-bit binary transmissions. When the server accepts and forwards a message containing 8-bit data, any 7-bit only intermediate network segments that detect the impending message may refuse to receive the message. This generally results in the message being returned-to-sender.
SMTP supports other commands in addition to the authentication and sending commands including:
- RSET- This command resets all data that has been entered into the SMTP server.
- HELP command-name- This command provides syntactical and other information about SMTP commands.
- VRFY recipient email address- Verifies an address as being acceptable by the system.
- EXPN alias-name- This command expands an alias and returns a list of recipients the alias will send to.
SMTP Sample Dialog
The following example demonstrates a dialog that could occur between two mail transfer agents transferring mail using SMTP.
Recipient < wait for connection on port 25 > Sender: < make TCP connection > Sender: EHLO Recipient: 250 server1.dart.com 250-HELP 250-EXPN 250-XREMOTEQUEUE 250-ETRN 250-PIPELINING 250-SIZE Sender: MAIL FROM: <Smith@Alpha.ARPA> Recipient: 250 OK Sender: RCPT TO: <Jones@Beta.ARPA> Recipient: 250 OK Sender: RCPT TO: <Green@Beta.ARPA> Recipient: 550 No such user here Sender: RCPT TO: <Brown@Beta.ARPA> Recipient: 250 OK Sender: DATA Recipient: 354 Start mail input; end with <CRLF> . <CRLF> Sender: Blah blah blah... Sender: ...etc. etc. etc. Sender: <CRLF>.<CRLF> Recipient: 250 OK Sender: QUIT Recipient: 221 closing channel < TCP connection ended >
Although PowerTCP Mail for .NET shields you from the low-level implementation described above, you can still access a communication log such as this one by using the Trace event.