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.


Send Mails from within a .NET 2.0 Application (2) Thiru ThangarathinamJune 10, 2005Mail C...
  • yorck
  • yorck
  • 2006年05月23日 00:17
  • 882

mysql 事件 存储过程 Can't create a PROCEDURE from within another stored routine

Can't create a PROCEDURE from within another stored routine. 出现了这个问题,说明你是在用某种工具在编写事件,那么在开头的时候,就不用再写...
  • hjm4702192
  • hjm4702192
  • 2013年01月25日 14:47
  • 7293

报错 The 'build' command is only supported from within a workspace.

原因:没有在指定的工作区进行命令的执行。 如我出现这个错误是在搭建Tensorflow时出现的 当时的命令为:bazel build -c opt //tensorflow/tools/pip_p...
  • nuannuanyingying
  • nuannuanyingying
  • 2017年06月16日 21:59
  • 1494

The 'build' command is only supported from within a workspace-SOLVED

I install tensorflow by python-pip. it works but when i run my project,  the IDE warns me that :"The...
  • hongxingabc
  • hongxingabc
  • 2017年08月09日 22:27
  • 1777


  • wq7570875
  • wq7570875
  • 2014年05月22日 11:48
  • 1793

IAR _ramfunc

来自: _ramfunc的意思就是把接下来的函数完全从FLASH复制到RAM里面...
  • u014183377
  • u014183377
  • 2014年11月13日 23:09
  • 797

ORCAD层次化设计遇到Cannot descend from a schematic page that is not contained within the hierarchy

层次化设计遇到Cannot descend from a schematic page that is not contained within the hierarchy 这个问题一般是由于...
  • songisgood
  • songisgood
  • 2015年12月05日 15:09
  • 1689

TensorFlow 研究实践 一

一、Caffe、TensorFlow、MXnet三个开源库对比 选择学习TensorFlow二、深...
  • forest_world
  • forest_world
  • 2016年05月03日 17:41
  • 15158

IAR _ramfunc

_ramfunc的意思就是把接下来的函数完全从FLASH复制到RAM里面执行,优点就是速度比在FLASH执行快多了,对实时性要求很高的操作有用。比如很多中断函数都会加_ramfunc ,但是RAM是有...
  • lan120576664
  • lan120576664
  • 2013年11月07日 14:28
  • 6077


QUESTION NO: 338 Within a resource-plan definition, what differentiates a top-level plan from a sub...
  • xuejiayue1105
  • xuejiayue1105
  • 2015年10月12日 19:26
  • 996
您举报文章:Send Mails from within a .NET 2.0 Application (1)