Software

Sending e-mail messages with ASP.NET applications

ASP.NET makes it easy to utilize e-mail in an application with the System.Web.Mail namespace. Let's take a closer look at putting this namespace to work in your applications.

E-mail is often a standard part of Web-based applications. You may use it to send logon information to users or even to send error messages to application administrators. ASP.NET makes it easy to utilize e-mail in an application with the System.Web.Mail namespace. Let's take a closer look at putting this namespace to work in your applications.

System.Web.Mail namespace

The Microsoft documentation provides a good overview of the System.Web.Mail namespace. It's composed of classes that allow you to create and send messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component. The actual message may be delivered via the SMTP (Simple Mail Transfer Protocol) mail service built into Microsoft Windows 2000 and beyond or through an arbitrary SMTP server. The classes in this namespace aren't restricted to an ASP.NET application.

The namespace includes three classes:

  • MailAttachment: Provides properties and methods for constructing an e-mail attachment.
  • MailMessage: Provides properties and methods for constructing an e-mail message.
  • SmtpMail: Provides properties and methods for sending messages using the CDOSYS message component.

MailAttachment class
The basic approach is the creation of a MailMessage object followed by sending it on its way via a SmtpMail object. The MailMessage class contains numerous methods and properties for working with an e-mail message. Properties such as From, Subject, and Body provide everything you need to create an e-mail message, but a SmtpMail object is still necessary for sending it on its way.

SmtpMail class
The SmtpMail class includes the SmtpServer property that gets or sets the name of the SMTP relay mail server to use to send messages, and the Send method actually sends the message. The Send method is overloaded. It allows a message to send using two approaches:

  • A MailMessage object is passed to the SmtpServer object.
  • Four string objects may be passed to the SmtpServer object with the first being the From field followed by the Recipient, Subject, and the message's Body.

You'll use the MailAttachment and SmtpMail classes together to create the necessary messages in your application, but make sure the Web server is properly configured to send a message via SMTP. Since IIS (Internet Information Services) is the most popular platform for ASP.NET applications, go ahead and use both the IIS and SMTP services to send messages from your application.

Using SMTP with IIS

You can set up both IIS and SMTP services via the Windows control panel. The SMTP service's role is to accept and deliver the messages using the server's configuration. It may deliver the messages directly, or utilize a smart host to deliver the message instead. When a smart host is enlisted, all messages are forwarded to it for delivery.

A little more information is appropriate for debugging. The SMTP service uses a directory structure to contain messages prior to delivery with the default directory being C:\Inetpub\mailroot. It contains numerous subdirectories including Queue, Drop, and Badmail. If you're unable to configure your instance of the SMTP Service for delivery, you can find the message in an EML file in the Queue subdirectory. The Badmail directory contains messages that that couldn't be delivered. Now, let's take a look at sending mail messages from your code.

Sending e-mail messages

To compose an e-mail message in your code, you need to start by creating an instance of the MailMessage class, as shown in the following C# snippet:

MailMessage msg = new MailMessage();

Be sure to include the System.Web.Mail namespace in your code:

using System.Web.Mail;

Once the object is instantiated, the various properties of the MailMessage class are used per your application. The following lines set the recipient, sender, and subject of the message:

msg.To = "test@test.com";
msg.From = "me@test.com";
msg.Subject = "Test Message";

The next step is setting our mail server via the SmtpServer object's SmtpServer property:

SmtpMail.SmtpServer = "smtp server name or address";

The final step is sending the message by passing our MailMessage object to the SmtpMail object's Send method:

SmtpMail.Send(msg);

The previous code used C#. Here's a more complete listing via a Web form's Page_Load event coded in VB.NET:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles MyBase.Load
Dim msg As MailMessage = New MailMessage()
msg.To = "test@test.com"
msg.From = "me@test.com"
msg.Subject = "Test message"
msg.Body = "Message body"
Try
SmtpMail.SmtpServer = " smtp server name or address "
SmtpMail.Send(msg)
Catch ex As HttpException
Response.Write("Error: " + ex.ToString())
Catch ex As Exception
Response.Write("Error: " + ex.ToString())
End Try
End Sub

Notice a try/catch block is used to catch any exceptions raised during e-mail message composition. The equivalent C# code follows:

private void Page_Load(object sender, System.EventArgs e) {
MailMessage msg = new MailMessage();
msg.To = "test@test.com";
msg.From = "me@test.com";
msg.Subject = "Test message";
msg.Body = "Body of the message";
try {
SmtpMail.SmtpServer = "smtp server name or address";
SmtpMail.Send(msg);
} catch (HttpException ex) {
Response.Write("HTTP Error: " + ex.ToString());
} catch (Exception ex) {
Response.Write("Error: " + ex.ToString());
} }

A straightforward approach to e-mail

The .NET platform makes the task of sending e-mail messages easier. The System.Web.Mail namespace includes everything necessary to send these messages--that is, except the actual SMTP server.

You must set up an SMTP server on the application's host box, or you must redirect it to the appropriate address. IIS provides one approach with its SMTP add-on service, allowing an SMTP server to be set up to handle the messages or to utilize a smarthost indicating what server will handle the processing.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

0 comments

Editor's Picks