Send email in ASP. NET 2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

it is very easy to send emails in ASP. NET 2.0. The class library for sending emails is mainly used in the namespace system. net. in mail , The namespace has two core classes:
: mailmessage: describes an email message. It has attributes such as from, to, subject, and body;
? smtpclie NT: sends a specified mailmessage instance to a specified SMTP server.

The general steps for sending an email are:
1.Create a mailmessage object;
2.From, to, subject, body, and other attributes assigned 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.This mailmessage is sent by sending the smtpclient object.

The followingCodeDemonstrate the email sending process:

String Mailservername =   " Smtp.126.com " ; // SMTP server for sending emails
String Mailfrom =   " Sender0624@126.com " ; // Sender's mailbox (the user name of the sender's mailbox must be used when 126 is used as the email server)
String Mailto =   " Supremezone@126.com " ; // Recipient email
String Subject =   " Send emails using code " ; // Email Subject
String Body =   " Here is the body of the email. " ; // Email body

Using (Mailmessage message =   New Mailmessage (mailfrom, mailto, subject, body ))
{
// Smtpclient is the body of the sent mail. This constructor informs smtpclient which SMTP server is used to send the mail.
Smtpclient mailclient =   New Smtpclient (mailservername );
// Grant the authentication instance to mailclient, that is, the user name and password used to access the SMTP server.
Mailclient. Credentials =   New Networkcredential ( " Sender0624 " , " 111111 " );
// Final sending Method
Mailclient. Send (Message );
}

AboveThe method is throughCode MethodThe framework in ASP. NET 2.0 considers thatConfiguration, That is, the application is implemented through config.ProgramTo reduce the hardware encoding of program parameters. This configuration section belongs to the <system.net> section.
Add the following configuration section to the Web. config file: < System.net >
<! -- For a third-party SMTP server, you must specify the username and password, and specify the sender email address from Based on the host.
Test shows that the from value must be the username value plus the specified smpt server, and must be specified
For a local SMTP server, you only need to specify defaultcredentials = "true ". -->
< Mailsettings >
< SMTP Deliverymethod = "Network" From ="Sender0624@ 126.com"   >
< Network Host = "Smtp.126.com" Port = "25" Username ="Sender0624" Password = "111111" />
</ SMTP >
</ Mailsettings >
</ System.net >

The next step is the program code. You will find that the configuration method is more concise than the code method, and hard encoding is avoided. SMTP information is read from the configuration file. Using (Mailmessage message =   New Mailmessage ())
{
Message. to. Add ( New Mailaddress ( " Supremezone@126.com " )); // Recipient email
Message. Subject =   " Send email through Configuration File Settings " ; // Email Subject
Message. Body =   " Here is the body of the email. " ; // Email body

Smtpclient mailclient =   New Smtpclient ();
Mailclient. Send (Message );
}

compared with the code method, we found that the configuration method code saves the credentials , the Code becomes quite concise, and more importantly, the information can be stored on the web. configuration in the config file, which greatly increases the flexibility of the program, which is recommended. note that the user name and password in the example are real. Theoretically, you can run the example program to send emails normally, however, when sending an email, the program throws an exception " the email address is unavailable. The server response is: ó ã» § ±» ë ø ¶ ", But If the credentials authentication information is changed to another 126 mailbox, but the email can be sent normally, and the cause is not found. Further Study on the Stability of smtpclient emails will be conducted, I also hope to get your guidance. supplement: an email is sent. " invalid character found in the mail header " exception. Check whether the computer name contains Chinese characters. This may be a Microsoft Bug. The solution is not to contain Chinese characters.

References:Analysis of email sending in ASP. NET 2.0,Small solution for sending emails in. NET 2.0.

Source code: Click to download the complete sample program (Sample Path :/Netfromwork/Sendmail. Aspx)

 

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.