The SmtpMail Class



The SmtpMail Class

The SmtpMail class, found in the System.Web.Mail namespace, allows you to send SMTP messages in your C# network programs. This section discusses the methods and properties of the SmtpMail class and shows some examples of typical uses in C# programs.

Class Methods and Properties

The SmtpMail class provides a .NET interface to the CDOSYS mail library on Windows 2000 and XP systems. It does not use a constructor to create an instance of the class. Instead, you must use the static class methods and properties to pass information to the CDOSYS library.

The SmtpMail class does contain the standard Equals(), GetHashCode(), and ToString() methods, but they’re rarely (if ever) used. The Send() method, on the other hand, gets plenty of use. The Send() method is overloaded, using two separate formats:

Send(MailMessage message)
Send(string from, string to, string subject, string body)

The first format allows you to send a MailMessage object. The MailMessage class is a self-contained e-mail message, created by populating the properties of the class with information related to the message, and the destination address(es). This process will be covered a little later in the section on the MailMessage class.

The second format allows you to send a raw message, manually specifying the typical e-mail message header fields:

  • From specifies the e-mail address of the sender.

  • To specifies the e-mail address of one or more recipients, separated by commas.

  • Subject specifies the topic of the e-mail message.

The final parameter of the Send() method is the actual body of the message. The body can be in either a plain text or HTML format.

The sole property of the SmtpMail class is SmtpServer. It’s a static property that specifies the address of a mail relay server to use for forwarding outgoing mail messages. By default, the SmtpServer property is set to the IIS SMTP service if it is installed. If IIS is not installed, the SmtpServer property is set to a null value and will produce an error when you attempt to send a message.

If you are using a relay mail server, you must set the SmtpServer property before you attempt to send messages:

SmtpMail.SmtpServer = "mailsrvr.myisp.net";

When this value is set, all outgoing mail messages will be relayed through this mail server. Of course, you must ensure that you are allowed to relay mail messages through the server, or they will be rejected.

Using the SmtpMail Class

The MailTest.cs program in Listing 13.1, demonstrates how to create a simple mail message and send it through a remote mail relay to the recipient.

Listing 13.1: The MailTest.cs program
Start example
using System;
using System.Net;
using System.Web.Mail;
class MailTest
{
  public static void Main()
  {
   string from = "jessica@myisp.net";
   string to = "katie@anotherisp.net";
   string subject = "This is a test mail message";
   string body = "Hi Katie, I hope things are going well today.";
   SmtpMail.SmtpServer = "192.168.1.150";
   SmtpMail.Send(from, to, subject, body);
  }
}
End example

The MailTest program is about as simple as they come. All it does is define the relay mail server using the SmtpServer property, define each of the required string fields, and use the Send() method to send the message. If all goes well, the intended recipient should receive the message in their mailbox.

If you try to run this on a Windows platform that does not use the CDO 2 library, you will get an Exception message. The exact message will vary, depending on which platform you run it on. On my Windows NT 4 workstation, it looks like this:

C:\>MailTest
Unhandled Exception: System.Web.HttpException: Could not create 'CDONTS.NewMail'
 object.
  at System.Web.Mail.LateBoundAccessHelper.get_LateBoundType()
  at System.Web.Mail.CdoNtsHelper.Send(String from, String to, String subject,
String messageText)
  at System.Web.Mail.SmtpMail.Send(String from, String to, String subject, Stri
ng messageText)
  at MailTest.Main()
C:\>

As you can see from the Exception message, the SmtpMail class tried to use the CDONTS library on the NT system but was unsuccessful. If you are writing code that will be run on more than one Windows platform, it is best to catch this Exception and produce your own informational message:

try
{
  SmtpMail.SmtpServer = "192.168.1.150";
  SmtpMail.Send(from, to, subject, body);
} catch (System.Web.HttpException)
{
  Console.WriteLine("Sorry, this application only works
           on Windows 2000and XP platforms.");
}

This simple program used the form of the Send() method that only sets the From:, To:, and Subject: header fields in the message. There are lots more header fields that can be defined and used in e-mail messages. The next section describes these fields and how you can use them in your mail messages.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows