February 28, 2021
Hot Topics:

Sending E-Mail with System.Web.Mail

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

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:

  • 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.

Future Columns

The next column has yet to be determined. If you have something in particular that you would like to see explained, please e-mail me at mstrawmyer@crowechizek.com.

About the Author

Mark Strawmyer, MCSD, MCSE, MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in the architecture, design, and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com.

# # #

Page 2 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