Code for sending emails through Gmail in ASP. net2.0

Source: Internet
Author: User
Tags mailmessage smtpclient gmail password

Here we mainly use Gmail. The reason is that when I use a Gmail mailbox to send emails, I encounter a small difficulty. When I use a mailbox such as 163, you have not encountered this problem.
In ASP. net2.0, sending mail is very simple. We mainly use several classes from the namespace system. net. Mail, mailmessage and smtpclient.
Core Code Is very concise, as follows: Copy code Code: string to = "Enter the recipient's email address here ";
String from = "Enter the sender's email address here ";
String subject = "the subject of the email written here ";
String body = @ "the email content here ";

Mailmessage message = new mailmessage (from, to, subject, body );
// Smtpclient client = new smtpclient ("smtp.gmail.com", 465 );
Smtpclient client = new smtpclient ("smtp.gmail.com", 587 );

Client. Credentials = new networkcredential ("Gmail account", "Gmail password ");
Client. enablessl = true;

Client. Send (Message );

As shown in the code, it is very easy to send an email. It is especially mentioned here because there are a few small points worth noting. Otherwise, it will be puzzling.
First, you need to enable the pop function for the Gmail account used to send emails.
Secondly, pay attention to the port numbers used by Gmail, which are 465 and 587. (if there are any other ports, I did not investigate them carefully. If there are any omissions, please refer to Hai Han ). According to the instructions in Gmail, I used port 465 at first and always timed out. The email cannot be sent successfully. However, when I use outlook, port 465 is also used to successfully send and receive emails. This confused me. After hovering around, you can get a new port number, which is 587. In ASP. NET, we use port 587 to successfully send emails. I don't understand why Gmail only mentions 465, but ignores 587.

Hope to help you

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.