Simple SMTP Client Design

 < Day Day Up > 



The Simple Mail Transfer Protocol (or SMTP) is another ASCII-based protocol that operates on the client/server principle. The protocol is a synchronous request/response in which a client connects to a server and performs a multistep SMTP dialog to transfer mail from the client to server.

An example SMTP transaction is shown in Figure 15.7; all client commands are shown in bold. After the client connects to the server, a salutation is sent from the server to announce that it’s ready for the SMTP dialog (along with some other information that can be simply ignored, such as the mail server version, time, and so on).

start figure

220 mailsite.com ESMTP Sendmail 9.8.3/9.9.7; Mon, 15 Nov 2002 22:23:05

HELO mydomain.com

250 mailsite.com Hello IDENT:time@[192.168.1.1], pleased to meet you

MAIL FROM: <me@mydomain.com>

250 <me@mydomain.com>... Sender ok

RCPT TO:<tim@mailsite.com>

250 <tim@mailsite.com>...Recipient ok

DATA

354 Enter mail, end with "." on a line by itself

From: me@mydomain.com

To: tim@mailsite.com

Subject: Test email

Content-Type: text/plain

This is a test email.

.

250 WAA14066 Message accepted for delivery

QUIT

221 mailsite.com closing connection

end figure

Figure 15.7: Sample SMTP dialog between a client and server.

Note that all messages received from the mail server are two-part messages. The first part of the message is a numeric status code that identifies the status of the server for this dialog. The second part is a text-readable string that may be ignored. From Figure 15.7, we see that the salutation includes a numeric error code of “220”; this represents normal status and is the indication to the mail client that it may continue with the transaction. The numeric codes differ, depending upon the stage of the SMTP transaction. We’ll use a simple subset of the available SMTP commands, and, therefore, don’t need to understand the full range of SMTP status codes.

Note 

The Simple Mail Transfer Protocol is documented under RFC 821. This RFC can be freely downloaded and its location is provided in the Resources section of this chapter.

The data flow for the SMTP client is a simple socket client with a send/recv pair for each SMTP dialog (see Figure 15.8).

click to expand
Figure 15.8: Simple SMTP client/server data flow.

After the server-side socket has been established (through socket/bind/listen/accept), the client can connect to the server using the connect API function. The server immediately emits the salutation string, which is received by the client using the recv API function. The client then announces itself to the server using the “HELO” command, which includes the domain of the client. The server responds to the “HELO” command with a hello-response string, but most importantly, a “250” status code. This code tells the client that it is ready for the rest of the e-mail transaction.

The client then specifies the source of the mail and to whom it is destined (using the “MAIL FROM” and “RCPT TO” commands, respectively). The “DATA” command specifies that the body of the e-mail is coming next. The client may then send many lines of text, followed by a ‘.’ on a line by itself to mark the end of the mail. The return numeric status code “250” specifies to the client that the server successfully completed the e-mail transaction and that the e-mail has been accepted for delivery.

The final command to be performed is to end the SMTP transaction. This is done using the “QUIT” command, and causes the server to send a status response and then close the session. The close API function is then used on both sockets to physically close the connection.

Each implementation of the simple SMTP client within the target language includes a simple API that permits the developer to specify the recipient of the e-mail, source of the e-mail, subject, and a message body. With this information, the SMTP client will connect to the recipient’s mail server and, using the SMTP protocol, deliver the e-mail.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net