Pages

Advertisement

Thursday, July 12, 2007

Sending E-Mail with System.Web.Mail

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:

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);
}
}
}
}

Sending a Message with an Attachment Sample Code

The following sample code contains a C#-based Windows Console application that shows how to send an e-mail message that includes an attachment. This is done by creating instances of the MessageAttachment class and then adding them to the message through the Attachments collection.

using System;
using System.Web.Mail;

namespace CodeGuru.SendMail
{
/// <summary>
/// console application to demonstrate sending e-mail with an
/// attachment.
/// </summary>
class TestMail
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
TestMail.SendAttachment("testuser@codeguru.com",
"mstrawmyer@crowechizek.com",
"Test Message Using CDOSYS",
"Hello World! This is a simple
message sent using CDOSYS.",
"c:\\myattachment.txt");
}

/// <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>
/// <param name="MessageAttachmentPath">Path to attachment
/// </param>
public static void SendAttachment(string MessageFrom,
string MessageTo,
string MessageSubject,
string MessageBody,
string MessageAttachmentPath)
{
// Create and setup the message
MailMessage message = new MailMessage();
message.From = MessageFrom;
message.To = MessageTo;
message.Subject = MessageSubject;
message.BodyFormat = MailFormat.Text;
message.Body = MessageBody;

// Create and add the attachment
MailAttachment attachment = new
MailAttachment(MessageAttachmentPath);
message.Attachments.Add(attachment);

try
{
// Deliver the message
System.Console.WriteLine("Sending outgoing message");
SmtpMail.Send(message);
}
catch( System.Web.HttpException exHttp )
{
System.Console.WriteLine("Exception occurred:" +
exHttp.Message);
}
}
}
}

Possible Enhancements

We have demonstrated how to send e-mail messages in a couple of ways. It is now up to you to think about ways in which you can utilize this functionality within your applications. Here are some ideas to consider on your own:

No comments:

Post a Comment