CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Saturday, September 02, 2006

Sending Email in ASP.NET 2.0

Introduction
Email serves as a ubiquitous, asynchronous notification and information distribution system. Not surprisingly, there are many web development scenarios where server-side code needs to generate an email and scuttle it off to the intended recipient. The email may be destined for a user of the website, informing them of their newly created user account, reminding them of their forgotten password, or emailing them an invoice. Or it may be destined for a web developer or site administrator, providing information of an unhandled exception that just transpired or user feedback.

Fortunately, ASP.NET makes sending email a breeze. The .NET Framework version 1.x included a number of classes in the System.Web.Mail class that allowed programmatically sending an email with a few scant lines of code. While this namespace and these classes still exist in the .NET Framework version 2.0, they have been deprecated in favor of new mail-related classes found in the System.Net.Mail namespace. (For an article on sending email in ASP.NET version 1.x, see Sending Email from an ASP.NET 1.x Web Page or consult www.SystemWebMail.com.)

In this article we'll look at the classes in the System.Net.Mail namespace and see how to send an email from an ASP.NET 2.0 page's code-behind class. We'll also look at specifying relay server information in Web.config and how this information can be used in some of the built-in ASP.NET server controls for sending emails (such as when a user creates an account or needs a password reminder/reset). Read on to learn more!

Exploring the Classes in the System.Net.Mail Namespace
There are 16 different classes in the System.Net.Mail namespace, all related to send email to a specified Simple Mail Transfer Protocol (SMTP) server for delivery. The two core classes in this namespace are: o a specified SMTP server.

When sending an email from an ASP.NET 2.0 page you will, typically:

  1. Create a MailMessage object
  2. Assign its properties
  3. Create an instance of the SmtpClient class
  4. Specify details about the SMTP server to use (if they're not already specified within Web.config)
  5. Send the MailMessage via the SmtpClient object's Send method

Find more topics for

Providing the SMTP Server's Details
Sending an Administrator Email Through a Feedback Web Page

Post a Comment