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>