How to send emails from ASP. NET 2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

Today, email has become an ubiquitous, asynchronous notification, and distributed messaging system. There are many web development sites. server-side code must generate an email and send it to the specified receiver. This email can be sent to some users in the outlet to notify them about their new user account, remind them of the password they forgot, or submit a list to them by email. Of course, this can also be for web developers or site administrators to provide them with a message about a public unhandled exception or user feedback.

Fortunately, ASP. NET makes sending emails very easy .. NET Framework Version 1.x contains many classes in the System. Web. Mail class, which allow sending emails programmatically with just a few lines of code. Although this namespace and these classes still exist in ASP.. NET 2.0, but they are outdated. net. in the Mail namespace, find a new class related to Mail support.

In this article, let's discuss the classes in the System. Net. Mail namespace and analyze how to send emails from the code-behind class on an ASP. NET 2.0 page. We also need. config specifies the relay server message and how to apply the message to some built-in ASP.. NET Server Control to send emails (for example, when a user creates an account or needs a password prompt/reset device ).

1. Explore classes in the System. Net. Mail namespace

There are 16 different classes in the System. Net. Mail namespace, all of which are related to sending emails to a specified Simple Mail Transfer Protocol (SMTP) server for further submission. There are two core classes in this namespace:
◆ MailMessage: describes an email message. It has attributes such as From, To, Subject, and Body;
◆ SmtpClient: sends a specified MailMessage instance to a specified SMTP server.

Typically, when you send an email from an ASP. NET 2.0 page, you need:

1) create a MailMessage object;

2) Assign attributes to it;

3) create an SmtpClient class instance;

4) specify details about how to use the SMTP server (if they are not specified in Web. config );

5) Send the MailMessage through the Send method of the SmtpClient object.

The preceding steps 1st and 2nd can be skipped because the Send method of the SmtpClient class can receive a MailMessage object or receive four strings to describe the from,, subject and body ).

Other classes in the System. Net. Mail namespace also provide more advanced email functions. For example, these classes can be used to add attachments to an email message, embed objects into an email, and specify SMTP Server Authentication messages; there are also some Exception Derived classes used to handle SMTP-specific exceptions. We will discuss more advanced usage of these other classes in future articles.

  1. HttpWorkerRequest object in ASP. NET
  2. Details about four ASP. NET statuses
  3. ScriptManager control in ASP. NET AJAX
  4. SuperPreview calls ASP. NET or PHP to render the webpage
  5. ScriptManager control in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.