Analysis of email sending in ASP. NET 2.0

Source: Internet
Author: User
Tags mailmessage smtpclient
I. Introduction

Today, email has become an ubiquitous, asynchronous notification, and distributed messaging system. There are many web development sites.CodeYou must generate an email and send it to the specified recipient. 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 an email via just a few lines of code programmatically. Although this namespace and these classes still exist in.. NET Framework Version 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 an email 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 ).

Ii. 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 it attributes;

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 (which respectively 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 willArticleTo discuss the more advanced usage of these other classes.

3. provide detailed information about the SMTP server

This email is sent to a friend from Outlook or gmail.ProgramUse a relay server to establish a connection and send the content of the email message (along with other messages, such as the email creation date, email format (text or HTML, etc.), recipient, and so on ). The relay server receives the message, connects to the receiver's SMTP server, and sends the message. Once the message is submitted, the receiver can (later) use a different protocol (such as IMAP or POP3) to extract the message.

Therefore, to send an email from an ASP. NET page, we need to provide messages 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 port 25), whether to use SSL when connecting your email message content to the relay server, and certificate (if necessary. If you can install a local SMTP service on your Web server, it can periodically monitor a special "drop-off" directory, send any message that appears in that directory. You can configure whether the smtpclient class sends its email messages to a separate relay server or whether the mail can be put into a specified check-out directory through the deliverymethod attribute.

The information of the relay server used by the smtpclient class can be specified programmatically through the attributes of the smtpclient class, or be concentrated in the web. config file. To use the web. config method, you can add a <> element to the <configuration> element. Then, add a <mailsettings> element that contains a <SMTP> element (which is specified in its <network> sub-element. See the following code:

<! -Add email settings to the <> element -->
<Network host = "relayserverhostname" Port = "portnumber" username = "username" Password = "password"/>
<System. Web>... </system. Web>

The host attribute contains relayserverhostname. If you are using an external relay server, the relayserverhostname may be similar to If the port number of the relay server is other than the typical port 25, you can specify it through the port attribute. Most external relay servers require some type of authentication (to prevent anonymous spam from sending their spam information through relay ). The username and password attributes must be provided when user name/password authentication is required.

Only a subset of the smtpclient attribute can be specified through settings in Web. config. To customize other smtpclient attributes (such as enablessl, timeout, and so on), you can set them programmatically when sending emails.
4. Send an administrator email to the web page

To demonstrate how to use the mailmessage and smtpclient classes to send an email, I have created a simple feedback page example. On this page, users are prompted to enter their email addresses, their feedback topics, and the corresponding feedback content.

<Table border = "0">
<B> your email: </B>
<Asp: textbox runat = "server" id = "usersemail" columns = "30"> </ASP: textbox>
<B> subject: </B>
<Asp: textbox runat = "server" id = "subject" columns = "30">
</ASP: textbox>
<TD colspan = "2">
<B> body: </B> <br/>
<Asp: textbox runat = "server" id = "body" textmode = "multiline" columns = "55" rows = "10">
</ASP: textbox>
<TD colspan = "2" align = "center">
<Asp: button runat = "server" id = "sendemail" text = "send feedback"/>

Once the user provides a feedback message and clicks the "send feedback" button, a return event occurs and the click event of this button is triggered. In the event processor, a mailmessage object is created and its to, from, subject, and body attributes are set based on the messages provided by the user. After creating a mailmessage object and filling in its attributes, the email is sent through 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 = ""
'(1) create a mailmessage instance
Dim mm as new mailmessage (usersemail. Text, toaddress)
'(2) attribute mailmessage value assignment
Mm. Subject = subject. Text
Mm. Body = body. Text
Mm. isbodyhtml = false
'(3) create an smtpclient object
Dim SMTP as new smtpclient
'(4) Send mailmessage (set using web. config)
SMTP. Send (mm)
End sub

In the code here, we do not need to set any attributes of the smtpclient class, because they have been specified in the web. config file (refer to the download source code in this article ).

V. Conclusion

Through further integration from ASP. net 1. other improvement functions of X, Asp. the email sending capability in. 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 removed from ASP. NET code and moved to the Web. config file, as we can see in this example. In addition, it provides better support for relay server authentication. In the next article, we will explore more advanced email usage, such as constructing HTML-format emails, sending attachments, and embedding objects in emails, handle exceptions related to SMTP/relay servers, and so on.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.