C # Programming Guide (7) C # mail via GMAIL

Source: Internet
Author: User
Tags mailmessage smtpclient gmail password

 

C # using the System. Net. Mail class to send emails is quite simple.

However, generally, mail servers require spam filtering. Therefore, you must perform POP3 verification before using the SMTP service,
However, the. NET Framework API does not support this verification function. Therefore, we recommend that you use Google's Gmail to send emails.

 

Send emails through Google's SMTP server.

 

First, you need to specify the Google SMTP server address: smtp.gmail.com

Second, enable the SSL secure connection.

Third, Google's user creden。 are required.

 

The following code replaces some variables with valid GMAIL accounts.

 

 1 string user = "zhangsan"; // replace it with your GMAIL user Name
2 string password = "123456"; // replace it with your GMAIL password
3 //
4 string host = "smtp.gmail.com ";
5 //
6 string mailAddress = "zhangsan@gmail.com"; // Replace with your GMAIL account
7 string ToAddress = "zhangsan@hotmail.com"; // target email address.
8 //
9
10
11 SmtpClient smtp = new SmtpClient (host );
12 smtp. EnableSsl = true; // enable the secure connection.
13 smtp. Credentials = new NetworkCredential (user, password); // create a user credential
14 smtp. DeliveryMethod = SmtpDeliveryMethod. Network; // use Network transfer
15 // create an email
16 MailMessage message = new MailMessage (mailAddress, ToAddress, "Test", "This is a Test Message ");
17 smtp. Send (message); // Send an email

 

 

The Code has been tested completely .. Replace the preceding variables with caution .. Haha

 

Code: Download

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.