Recently, C # is used to implement free SMTP server (Gmail) mail and post 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 felt this was very unstable. So I switched to Gmail's SMTP service and 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 want to send email
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 send success -->
</SMTP>
</Mailsettings>
</System.net>

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.