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.
- HttpWorkerRequest object in ASP. NET
- Details about four ASP. NET statuses
- ScriptManager control in ASP. NET AJAX
- SuperPreview calls ASP. NET or PHP to render the webpage
- ScriptManager control in ASP. NET