Home Previous Section Next Section

SOAP in E-mail: SMTP

SMTP (Simple Mail Transfer Protocol) is the protocol for sending e-mail messages. It is complemented by other protocols such as POP3 (Post Office Protocol) for receiving e-mail messages. Of course, just as HTTP isn't merely for HTML, SMTP can also be used for non-user-based e-mails, such as ones that contain SOAP messages.

Typically, SMTP e-mail messages look similar to HTTP messages, but the most striking difference is how conversational SMTP is. It is a connection-oriented protocol, and as such, it tries to reliably deliver its e-mail. The following example illustrates this:

Client: MAIL FROM:<Keithba@microsoft.com> 
Server: 250 OK

Client: RCPT TO:<Keith@microsoft.net>
Server: 550 No such user here

Client: RCPT TO:<KeithBa@msn.com>
Server: 250 OK

Client: DATA
Server: 354 Start mail input; end with <CRLF>.<CRLF>
Client: This is an e-mail message.
Client: This is more of the e-mail.
Client: <CRLF>.<CRLF>
Server: 250 OK

As you can see, SMTP creates an actual dialog that is used to send the messages. This ensures much greater reliability in the sending of messages. Generally, SMTP is used over TCP, although other transports are possible and permitted in the SMTP specification. With SMTP over TCP, port 25 should be used. Also, all SMTP data is 7-bit ASCII, with the high-order bit in the byte cleared to 0.

Notice that SMTP allows for failure, such as not finding a particular user. In the preceding example, the error was returned and that was the end of it. Another possible response would indicate that the message sender should try another SMTP server. This response is highlighted in the following example:

Client: MAIL FROM:<Keithba@microsoft.com> 
Server: 250 OK

Client: RCPT TO:<Keith@microsoft.net>
Server: 551 User not local; please try <OtherMailServer@microsoft.com>

Client: RCPT TO:<KeithBa@msn.com>
Server: 250 OK

Client: DATA
Server: 354 Start mail input; end with <CRLF>.<CRLF>
Client: This is an e-mail message.
Client: This is more of the e-mail.
Client: <CRLF>.<CRLF>
Server: 250 OK

Even better, the server can indicate that it will forward the message itself:

Client: MAIL FROM:<Keithba@microsoft.com> 
Server: 250 OK

Client: RCPT TO:<Keith@microsoft.net>
Server: 251 User not local; will forward to <OtherServer@microsoft.com>

Client: RCPT TO:<KeithBa@msn.com>
Server: 250 OK

Client: DATA
Server: 354 Start mail input; end with <CRLF>.<CRLF>
Client: This is an e-mail message.
Client: This is more of the e-mail.
Client: <CRLF>.<CRLF>
Server: 250 OK

The .NET Framework has a class for sending SMTP mail messages when building Web applications. You will find it in the System.Web.Mail namespace, and it includes support for both building mail messages and sending them. This functionality is based on the SMTP capabilities found in the Windows 2000 operating system and later, excluding home editions.

Once you have created your Web application, you will need to import the System.Web.Mail namespace using the following code:

using System.Web.Mail; 

Next, you create a MailMessage object that contains the message you are sending, such as the following:

MailMessage message = new MailMessage(); 
message.To = "Keithba@microsoft.com";
message.From = "KeithBa@microsoft.com";
message.Subject = txtSubject.Text;
message.Body = txtMessage.Text;
message.BodyFormat = MailFormat.Text;

Finally, you merely set the server name and send the message!

SmtpMail.SmtpServer = "localhost"; 
SmtpMail.Send( message );

You also can send files with your SOAP message, by using the Mail Attachment class:

message.Attachments.Add( new MailAttachment("c:\\SomeFile.txt") ); 
    Home Previous Section Next Section