. NET 2.0 email sending Method

Source: Internet
Author: User

In. net Framework 1.x we need to use system. web. the class in the mail namespace to send emails, but the function is relatively weak. For example, your email server needs to verify before sending emails. in NET 1.1, the followingCodeFor additional configuration.

  Mail. Fields. Add (  "  Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate  "
, " 1 " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername "
, " My_username_here " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword "
, " Super_secret " );

For how to send emails under. Net 1.x, see:
Http://blog.joycode.com/joy/archive/2004/01/14/11405.aspx

In. NET Framework 2.0, the system. net. Mail namespace provides support for mail operations, which is more powerful. For example, if your email server needs verification before sending the email, the code is simply as follows:

 Public static void sendsmtpemail (string strsmtpserver, string strfrom, string strfrompass, string strto, string strsubject, string strbody)
{
System. net. Mail. smtpclient client = new smtpclient (strsmtpserver );
Client. usedefacrecredentials = false;
Client. Credentials = new system. net. networkcredential (strfrom, strfrompass );
Client. deliverymethod = smtpdeliverymethod. Network;

System. net. Mail. mailmessage message = new mailmessage (strfrom, strto, strsubject, strbody );
Message. bodyencoding = system. Text. encoding. utf8;
Message. isbodyhtml = true;

Client. Send (Message );
}

You can modify attributes such as usedefaultcredentials credentials deliverymethod to easily support sending emails in various situations.

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.