First:
// Emailaddress: Email Recipient address
// Mailcontent subject content
// Mailtitle
// Mailsubject subject
Public bool Sendmail (string emailaddress, string mailcontent, string mailtitle, string mailsubject ){
Mailmessage onemail = new mailmessage ();
String myemail = "aaaa@163.com"; // email address of the sent Email
String mypwd = "11111111"; // email sending Password
Onemail. bodyencoding = system. Text. encoding. utf8;
Onemail. isbodyhtml = true;
Onemail. From = new mailaddress (myemail );
Onemail. to. Add (New mailaddress (emailaddress ));
Onemail. Subject = mailsubject;
Onemail. Body = mailcontent;
Onemail. bodyencoding = system. Text. encoding. utf8;
Smtpclient Clint = new smtpclient ("mail.iapechina.com"); // the server that sends the email
Clint. usedefacrecredentials = false;
Clint. Credentials = new system. net. networkcredential (myemail, mypwd );
Clint. deliverymethod = smtpdeliverymethod. Network;
Try
{
Clint. Send (onemail );
Return true;
}
Catch (exception ex ){
Return false;
}
}
Second:
Make the following settings in Web. config:
<Configuration>
<System.net>
<Mailsettings>
<SMTP from = "aaaa@163.com"> <! -- Email address of the email to be sent -->
<Network host = "mail.iapechina.com" Password = "1111111" Port = "25" username = "aaaa@163.com" defaultcredentials = "false"/>
</SMTP>
</Mailsettings>
</System.net>
</Configuration>
// Emailaddress: Email Recipient address
// Mailcontent subject content
// Mailtitle
// Mailsubject subject
Private bool Sendmail (string emailaddress, string mailcontent, string mailtitle, string mailsubject ){
Mailmessage onemail = new mailmessage ();
Onemail. bodyencoding = system. Text. encoding. utf8;
Onemail. isbodyhtml = true;
Onemail. to. Add (New mailaddress (emailaddress ));
Onemail. Subject = mailsubject;
Onemail. Body = mailcontent;
Onemail. bodyencoding = system. Text. encoding. utf8;
Smtpclient Clint = new smtpclient ();
Try
{
Clint. Send (onemail );
Return true;
}
Catch (exception ex ){
Return false;
}
}
Summary. in. NET 2.0, use system. web. sending emails via mail is very simple. You can also get detailed exception information when errors occur, but sometimes problems may make you confused. Most often, when your machine is installed with the anti-virus firewall, you may not be able to send an email, but the prompt information makes it impossible to diagnose the problem, the message is rejected. In this case, the firewall rejects the use of port 25, resulting in failure to communicate with the SMTP server. The solution is to close it. I believe there are many cases on the Internet. Pay special attention to the fact that the machine that sent the email must be on the Internet directly. This is especially impressive because the company uses a proxy to access the Internet. The above Code Note that the from Value of the mailmessage object must be the same as the from Value in the configuration file. You can try it here.