January 25, 2021
Hot Topics:

Sending E-Mail with System.Web.Mail

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Welcome to the next installment of the .NET Nuts & Bolts column. In this column, we'll explore sending e-mail from within applications. This will involve utilizing classes contained in the System.Web.Mail namespace.

Collaboration Data Objects

Collaboration Data Objects for Windows 2000 (CDOSYS) is a Microsoft messaging component that allows for standards-based e-mail messages to be constructed and sent. It is a replacement of the Collaboration Data Objects for NTS (CDONTS), which, as you can probably guess by the name, was for Windows NT. CDONTS is included with Windows 2000 for backwards compatibility, but Windows XP, Windows Server 2003, and beyond do not include or support the use of CDONTS. Thus, any applications that send messages using CDONTS must be migrated to use CDOSYS. It provides the same functionality and is just as easy to use.

In addition to serving as a replacement, CDOSYS introduces some new functionality that was not previously available in CDONTS. Some of the functionality includes:

  • Ability to post messages to newsgroups
  • Control of MIME body structure for messages
  • Reply and forward functionality
  • Transport event sink to allow responding to events

The System.Web.Mail namespace contains classes that interact with CDOSYS to construct and send the message(s).

Using IIS and SMTP Service

In order for CDOSYS to send e-mail or other messages from your application, you need to enlist the services of IIS with the SMTP Service installed. Both are available in Windows 2000/XP through the Control Panel -> Add/Remove Programs -> Add/Remove Windows Components option. The job of the STMP Service is to accept and deliver the messages, based on the configuration. The service can attempt to deliver the messages directly, or it can utilize a smart host to deliver the message instead. When a smart host is enlisted, all messages are forwarded to it for delivery. You need to have IIS and the SMTP service installed and configured.

The SMTP Service uses a directory structure to contain messages prior to delivery. The default directory is C:\Inetpub\mailroot. This folder contains a number of subdirectories such as Queue, Drop, and Badmail. If you are unable to configure your instance of the SMTP Service for delivery, you can find the message in a *.EML file in the C:\Inetpub\mailroot\Queue directory. This technique can be useful when creating messages offline.

Sending a Message

As previously mentioned, sending an e-mail is a relatively simple thing to do. The System.Web.Mail.MailMessage class represents the message to be sent. E-mail messages are constructed by using instances of this class. This class contains properties such as To, From, and Subject that allow you to control the message being sent. Attachments can be created through instances of the System.Web.Mail.MailAttachment and then added to the MailMessage through the Attachments collection of the MailMessage. The message is then delivered through the System.Web.Mail.SmtpMail class.

Sending a Message Sample Code

The following sample code contains a C#-based Windows Console application that shows how to send an e-mail message. By not specifying that the SmtpMail.SmtpServer property is not set, the localhost is used by default. You need to make sure to add a reference to the System.Web.dll because this is a console application and not an ASP.NET application.

using System;using System.Web.Mail;namespace CodeGuru.SendMail{  /// <summary>  /// Test console application to demonstrate sending e-mail.  /// </summary>  class TestMail  {    /// <summary>    /// The main entry point for the application.    /// </summary>    [STAThread]    static void Main(string[] args)    {      TestMail.Send("testuser@codeguru.com",                    "mstrawmyer@crowechizek.com",                    "Test Message Using CDOSYS",                    "Hello World!  This is a simple message sent                     using CDOSYS.");    }    /// <summary>    /// Send a message using the .NET wrapper for Collaborative Data    /// Objects (CDO).  This method should be used when sending to a    /// single recipient only; otherwise, the list of recipients    /// will be known.    /// </summary>    /// <param name="MessageFrom">Message originator</param>    /// <param name="MessageTo">Message receipent</param>    /// <param name="MessageSubject">Message subject</param>    /// <param name="MessageBody">Message body</param>    public static void Send(string MessageFrom,                            string MessageTo,                            string MessageSubject,                            string MessageBody)    {      MailMessage message = new MailMessage();      message.From        = MessageFrom;      message.To          = MessageTo;      message.Subject     = MessageSubject;      message.BodyFormat  = MailFormat.Text;      message.Body        = MessageBody;      try      {        System.Console.WriteLine("Sending outgoing message");        SmtpMail.Send(message);      }      catch( System.Web.HttpException exHttp )      {        System.Console.WriteLine("Exception occurred:" +                                 exHttp.Message);      }    }  }}

Page 1 of 2

This article was originally published on February 9, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date