Sample Code for sending an email using ASP. net2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

A foreign English website has detailed code descriptions:

Address: http://www.systemnetmail.com/

 

The following code is an example of my reference:

 

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. net;
Using system. net. mail;

Namespace email_test.aspx
{
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String emailtitle = "test email ";
String toemail = "seagreen7@yeah.net ";
String mycontent = "this is the test email content ";
String myresult = sendhtmlemail (emailtitle, toemail, mycontent );
If (myresult = "OK ")
{This. label1.text = "congratulations, the email has been successfully sent to" + toemail ;}
Else
{This. label1.text = "sorry, email sending failed. Please check the configuration information of the web. config file system.net node. ";}
}
Public static string sendhtmlemail (string emailtitle, string destemail, string emailcontent)
{
Try
{
// Read the configuration information sent by mail in Web. config
// In the code, we do not need to set any attributes of the smtpclient class because they have been specified in the web. config file.

// Create the mail message
Mailmessage mail = new mailmessage ();

// Set the addresses
Mail. to. Add (destemail );

// Set the content
Mail. Subject = emailtitle;

// Screen scrape the html
String html = emailcontent;
Mail. Body = HTML;
Mail. isbodyhtml = true;

// Send the message
Smtpclient SMTP = new smtpclient ();
SMTP. Send (Mail );
}
Catch (exception E)
{
Return "fail <br>" + E. tostring (); // failed to send, return fail
}
Return "OK"; // if the message is sent successfully, OK is returned.

}
}
}


 

The content of web. config is as follows:

 

<System.net>
<Mailsettings>
<! -- Set email sending and set the email address and password here to your own. -->
<SMTP from = "yours@126.com">
<Network host = "smtp.126.com" Port = "25" username = "yours@126.com" Password = "123456" defaultcredentials = "false"/>
</SMTP>
</Mailsettings>
</System.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.