One of the ASP.net 2.0 email parsing

Source: Internet
Author: User
Tags config contains mail mailmessage port number smtpclient
asp.net

   First, Introduction

Today, e-mail has become a ubiquitous, asynchronous notification, and distributed messaging system. There are many web development sites where server-side code needs to generate an e-mail message and send it to the specified recipient. The e-mail message can be sent to some users in the network, notifying them of their new user accounts, reminding them of their forgotten passwords, or submitting them to a list in the form of a message. Of course, this can also be for Web developers or site administrators, to provide them with information about an unhandled exception or user feedback that has just been exposed.

Luckily, ASP. NET makes sending e-mail very easy. NET Framework version 1.x includes many classes in the System.Web.Mail class that allow you to programmatically send an e-mail message through just a few lines of code. Although this namespace and these classes still exist in the. Net Framework version 2.0, but they are obsolete and you can find new classes in the System.Net.Mail namespace that are related to mail support.

In this article, let's explore the related classes in the System.Net.Mail namespace and analyze how to send an e-mail message from a asp.net 2.0 page Code-behind class. We also specify relay server messages in Web.config and how to apply this message to some built-in ASP.net server controls for sending e-mail messages (for example, when a user creates an account or needs a password prompt/reset device).

   Ii. exploring classes in the System.Net.Mail namespaces

There are 16 different classes in the System.Net.Mail namespace that are related to sending e-mail messages 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,body;

· SmtpClient: Sends a specified MailMessage instance to a specified SMTP server.

Typically, when you send an email from a asp.net 2.0 page, you want to:

1. Create a MailMessage object;

2. Assign to it attributes;

3. Create a SmtpClient class instance;

4. Specify details about the use of this SMTP server if they are not specified in web.config;

5. Send the MailMessage via the Send method of the SmtpClient object.

Of these, steps 1th and 2nd above can be skipped because the Send method of the SmtpClient class can receive a MailMessage object or receive four strings (describing the from,to,subject and body parts of the e-mail message separately).

Other classes in the System.Net.Mail namespace also provide more advanced e-mail functionality. For example, these classes can be used to add attachments to an e-mail message, embed objects into an email, specify SMTP server authentication messages, and some exception derived classes to handle SMTP-specific exceptions. We will discuss more advanced usage scenarios for these other classes in future articles.

   Iii. Providing detailed information about the SMTP server

When you send an e-mail message to a friend from Outlook or Gmail, the e-mail program uses a relay server to establish a connection and send the content of the e-mail message (along with some other messages, such as e-mail creation date, e-mail format (text or HTML, and so on), Receiver, etc.). The relay server receives the message and then connects to the recipient's SMTP server and sends the message. Once the message is committed, the recipient can (later) use a different protocol (for example, IMAP or POP3) to extract the message.

Therefore, in order to send an e-mail message from a asp.net page, we need to provide a message to the SmtpClient class about the relay server. With the host name of the relay server, you can specify the following information: the corresponding port number (typically using port 25), whether to use SSL when connecting your e-mail message content to the relay server, and the authentication certificate (if necessary), and so on. As a choice, if you can install a local SMTP service on your Web server, it can periodically monitor a particular "drop-off" directory and send any messages that appear in that directory. You can configure whether the SmtpClient class sends its e-mail message to a separate relay server or whether the message can be put into a specified checkout directory via the Deliverymethod attribute.

The relay server information used by the SmtpClient class can be specified programmatically through the properties of the SmtpClient class, or it can be concentrated in a Web.config file. In order to use the Web.config method, you can add a <system.net> element within the <configuration> element. Then, add a <mailSettings> element that contains a <smtp> element whose settings are specified in its <network> child element. Please refer to the following code:

<configuration>
<!-add e-mail settings to the <system.net> element-->
<system.net>
<mailSettings>
<smtp>
<network host= "Relayserverhostname" port= "PortNumber" username= "userName" password= "password"/>
</smtp>
</mailSettings>
</system.net>
<system.web> ... </system.web>
</configuration>
Where the Host property contains Relayserverhostname. If you are using an external relay server, the Relayserverhostname may be similar to smtp.yourisp.com content. If the port number of the relay server is a number other than the typical port 25, you can specify it by using port property. Most external relay servers require some type of authentication (to prevent anonymous spam from relaying their spam messages). You need to provide username and password properties when you need a username/password authentication.

Only a subset of the SmtpClient attribute can be specified through the settings in Web.config. To customize other SmtpClient attributes (such as enablessl,timeout, etc.), you can set them programmatically when sending e-mail.

  Iv. Send admin Email via feedback web page

To illustrate the use of the MailMessage and SmtpClient classes to send an e-mail message, I have created a simple feedback page example. In this page, users are prompted to enter their email address, their feedback topic and the corresponding feedback content.

















Your Email:



Subject:



Body:





Once the user provides a feedback message and clicks on the "Send Feedback" button, a postback occurs and the Click event of the button fires. Within the event handler, a MailMessage object is created, and its to,from,subject and body properties are set according to the message provided by the user. After the MailMessage object is created and its properties are populated, the e-mail message is sent out via the Send method of the SmtpClient class.

Protected Sub Sendemail_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Sendemail.click
'!!! Update this value to your email address
Const toaddress as String = "you@youremail.com"
' (1) Create MailMessage instance
Dim mm as New mailmessage (Usersemail.text, toaddress)
' (2) attribute MailMessage assignment
Mm. Subject = Subject.text
Mm. BODY = Body.text
Mm. isbodyhtml = False
' (3) Create SmtpClient Object
Dim SMTP as New smtpclient
' (4) Send MailMessage (will use Web.config settings)
Smtp. Send (mm)
End Sub
In the code here, we do not need to set any properties of the SmtpClient class, as they are already specified in the Web.config file (please refer to the download source code in this article).


   v. Conclusion

By further combining other enhancements from ASP.net 1.x, the e-mail delivery capability in ASP.net 2.0 is further updated and moved to a new namespace-system.net.mail. In asp.net 2.0, the relay server settings can be easily detached from the ASP.net code and moved into the Web.config file, as we have seen in this example. Also, there is better support for relay server authentication. In the next article, we'll explore more advanced e-mail usage, such as constructing HTML-formatted e-mail, sending attachments, embedding objects inside e-mail, handling exceptions related to smtp/relay servers, and so on.

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.