C # use the free SMTP server (Gmail) to send emails and paste the code in segments.

Source: Internet
Author: User
Tags mailmessage smtpclient

Note: I was planning to use Sina ,(SMTP.Sina.com), but after testing, I feel this is very unstable. So I switched to the SMTP service of Gmail, and it felt very stable and fast. Record it to help later!

Method 1
Bytes ------------------------------------------------------------------------------------------------------
// Using system. net. mail;
Mailmessageage message = new mailmessageage ();
Message. From = new mailaddress ("User@gmail.com", "Your displayname ");
Message. to. Add (New mailaddress ("To@gmail.com"); // the email you wantTo sendEmail
Message. Subject = "A test email"

Message. isbodyhtml = true;
Message. bodyencoding = system. Text. encoding. utf8;
Message. Body = "This is just a simple test! <Br> Jack"
Message. Priority = mailpriority. High;

Smtpclient client = new smtpclient ("smtp.gmail.com", 587); // 587; // port used by Gmail
Client. Credentials = new system. net. networkcredential ("User@gmail.com", "*****"); // your user name & Password
Client. enablessl = true; // encrypted by SSL
Object userstate = message;
Try
{
Client. Send (Message );
Response. Write ("mail sent to" + message. to. tostring () + "<br> ");
}
Catch (exception ee)
{
Response. Write (EE. messageage + "<br>" + ee. innerexception. messageage );
}

Method 2
Bytes ------------------------------------------------------------------------------------------------------

// Http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx
// Using system. net. mail;
Mailmessage message = new mailmessage ();
Message. From = new mailaddress ("User@gmail.com ");

Message. to. Add (New mailaddress ("to@gmail.com "));

Message. Subject = "this is my subject"
Message. Body = "this is the content"
Smtpclient client = new smtpclient ();
Client. enablessl = true;

Try
{
Client. Send (Message );
Response. Write ("mail sent to" + message. to. tostring () + "<br> ");
}
Catch (exception ee)
{
Response. Write (EE. Message );
}

// In web. config

<System.net>
<Mailsettings>
<SMTP from = "from@gmail.com">
<Network host = "smtp.gmail.com" Port = "587" username = "User@gmail.com" Password = "Your PWD"/>
<! -- If has 'defaultcredentials = "true" ', using Gmail can not sendSuccess-->
</SMTP>
</Mailsettings>
</System.net>

Jack is here: Focus on. NET, Asp.net, C #, LINQ and so on
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.