Sending E-Mail with System.Web.Mail
Collaboration Data Objects
- 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
Using IIS and SMTP Service
Sending a Message
Sending a Message Sample Code
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
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
- E-mail alerts—when a fatal or unrecoverable application error occurs, your application could e-mail information to a designated location so that it is immediately known.
- Build a Web-based contact form—you can allow users to send customer feedback by filling out a Web form and then programmatically e-mailing it to the appropriate contact(s).
- Subscription service—when sending mail by using CDOSYS for a subscription-type service, you may want to send multiple messages instead of a single message with all of the recipients. When a message has too many recipients, it can drastically slow processing as all of the recipients are processed. It is often better to break the list of recipients into multiple lists and send multiple messages.
- Send messages using Bcc—when sending mail using by CDOSYS for a subscription-type service, you may want to address messages using the Bcc instead of To. This will keep the list of recipients unknown to all of those that receive it.
- Send HTML-formatted mail—the message body format can be set to HTML. This will allow the body of the message to be sent in HTML format rather than plain text.
No comments:
Post a Comment