Send Mails from within a .NET 2.0 Application (1)

原创 2006年05月23日 00:09:00

Thiru Thangarathinam
June 10, 2005

Most enterprise applications today need to send mail. This means the platform should provide the necessary support for applications to send these mails. In the case of .NET applications, .NET provides excellent support for sending e-mails through a set of intuitive classes. This article introduces the new ways of sending mails through the System.Net.Mail namespace by showing examples. You also will see the steps involved in sending attachments as part of the mails, as well as sending mails to multiple recipients in the form of CC and BCC lists. Finally, it explains the use of XML and XSL in formatting the bodies of HTML-based mails.

.NET 2.0 Mail Functions

Microsoft made some interesting changes in how you can send e-mail in version 2.0 of the .NET Framework. With .NET 1.x versions, you had to utilize the classes contained in the System.Web.Mail namespace for sending mails. Now, with the release of .NET 2.0, the System.Web.Mail namespace is obsolete and its functionality is now available in the System.Net.Mail namespace. This is a welcome change because the mail functionality was intended to be used for all .NET applications and should not have been under System.Web assembly.

In addition to moving the functionality under the System.Net.Mail namespace, the .NET team also completely rebuilt the implementation of SMTP functionality. There are new classes, properties, and methods that provide an elegant and clean way of performing operations related to sending mails. This long list of new enhancements includes quality improvements, different methods, and asynchronous support out of the box.

Now that you have a general understanding of the support provided by .NET 2.0 for sending mails, let us dive deep into the actual classes contained in the System.Net.Mail namespace.

System.Net.Mail Namespace

The System.Net.Mail namespace contains all the classes required for sending mail from within a .NET application. The classes contained in this namespace work a bit differently from the classes in the Web namespace. Table 1 describes the classes contained in the System.Net.Mail namespace.

Table 1. Important Classes in the System.Net.Mail Namespace

Class Description
Attachment Represents an attachment that is sent with a mail and is used in conjunction with the MailMessage class
MailAddress Represents the address of the sender or recipient (To, CC, and BCC)
MailMessage Represents an e-mail message that can be sent using the SmtpClient class and exposes properties such as From, To, CC, BCC, Attachments, Subject, and Body to formulate the message's contents
SmtpClient Allows applications to send emails using the SMTP
SmtpException Represents the exception that is thrown when the SmtpClient is not able to send the message

Of the classes that Table 1 lists, MailMessage and SmtpClient are the two core classes that you need to work with to send the simplest of mails.


The SmtpClient class handles the actual sending of e-mail. For a simple mail, you can directly pass in the To address, from, subject, and body of the mail to one of the SmtpClient's overloaded send methods. However, this approach works only for simple mails that don't need any advanced formatting, which is normally provided by the MailMessage class.

To have complete control over the mail message, you need to create an instance of the MailMessage class and set its properties to appropriate values. For the purposes of this example, create a Visual C# Class Library project named MailServer using Visual Studio 2005. Once the project is created, rename the default class to MailService. To the MailService class, add a method named SendMail and modify its code to look as follows (an example of how to use the MailMessage class to create a mail message and then send it using the SmtpClient object):

public void SendMail(string from, string to,
                     string subject, string body)
   string mailServerName = "";
      //MailMessage represents the e-mail being sent
      using (MailMessage message = new MailMessage(from,
             to, subject, body))
         message.IsBodyHtml = true;
         SmtpClient mailClient = new SmtpClient();
         mailClient.Host = mailServerName;
         mailClient.UseDefaultCredentials = true;
         mailClient.DeliveryMethod =
         //Send delivers the message to the mail server
   catch (SmtpException ex)
      throw new ApplicationException
         ("SmtpException has oCCured: " + ex.Message);
   catch (Exception ex)
      throw ex;

In the above code, at the time of creating an instance of the MailMessage class, you pass in the From, To, Subject, and body of the message as arguments. Then, you set the IsBodyHtml property to true to indicate the type of mail you want to send. After that, you create an instance of the SmtpClient object and then set its properties, such as Host, UseDefaultCredentials, and DeliveryMethod, to appropriate values. Finally, you send the mail using the Send method, which sends out the mail in a synchronous manner. Note that at the time of sending a mail, if an SMTP exception occurs, it will be handled in the SmtpException catch block. All other exceptions are caught by the generic Exception block. Also note that in the above method, you directly set the To property of the MailMessage object to the supplied value. If you are sending mails to multiple recipients, you need to create that many instances of MailAddress objects and add them to the MailMessage.To property, which represents a collection of MailAddressCollection objects.

Now that you have created the SendMail method, it's time to call that method from a client application.




Repeater within ASP.NET 2.0 Gridview

Introduction This article and code snippet here shows how to bind a repeater control and Gridview c...

你的.net 2.0 真的能与1.1 安全正确地运行在同一台电脑上吗? 小心Server Application Unavailable 错误

答案 当然是 肯定的。但不作任何处理的默认情况下,会出问题! 各位在同一电脑上同时安装并运行.net 1.1 和 .net 2.0 两个版本的朋友要小心啊! 您的项目是不是时不时会...

你的.net 2.0 真的能与1.1 安全正确地运行在同一台电脑上吗? 小心Server Application Unavailable 错误

答案 当然是 肯定的。但不作任何处理的默认情况下,会出问题!    各位在同一电脑上同时安装并运行.net 1.1 和 .net 2.0 两个版本的朋友要小心啊!    您的项目是不是时不时会出现 S...

[翻译]ASP.NET 2.0中的健康监测系统(Health Monitoring)(1) - 基本应用

原文地址: [原文源码下载] [译者改后源码下载] [翻译]ASP.NET ...

Quartz.Net 2.0 bate1 使用

下载后发现在XP下无法使用 安装,编译后找到生成目录在命令行下运行下面的命令进行安装与卸载 按照:>  Quartz.Server.exe install 卸载:>  Quartz.Server...
  • e_wsq
  • e_wsq
  • 2012-02-28 00:45
  • 422

Quartz.NET 2.0 学习笔记(1) :Quartz.NET简介

Quartz.NET 项目地址 Quartz.NET 2.0 学习笔记(1) :Quartz.NET简介 Quartz.NE...
  • hao_ds
  • hao_ds
  • 2015-01-05 10:03
  • 1021 2.0 自定义伪静态源码

根据微软官方伪静态UrlRewrite.dll源码,自己改写应用进项目中。  1、首先,我们写个用于HttpModule请求的类 RolesProvider using System; us...