Demo of sending mail based on. net2.0 system. net. Mail

Source: Internet
Author: User
Tags mailmessage smtpclient

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.

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.