code for sending mail through Gmail in asp.net2.0-practical tips

Source: Internet
Author: User
Tags documentation mailmessage smtpclient gmail password
Here we mainly use Gmail, the reason is because, I use Gmail mailbox to send mail, encountered a small difficulty, and the use of 163 mailbox, did not encounter 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.
The core code is very concise, as follows:
Copy Code code as follows:

String to = "Fill in the recipient's email address here";
String from = "Fill in the sender's email address here";
String subject = "topic for writing messages here";
String BODY = @ "Write the contents of the message here";

MailMessage message = new MailMessage (from, to, subject, body);
SmtpClient client = new SmtpClient ("", 465);
SmtpClient client = new SmtpClient ("", 587);

Client. Credentials = new NetworkCredential ("Gmail account", "Gmail password");
Client. Enablessl = true;

Client. Send (message);

As the code shows, this email is a simple thing to do. In particular, it is because there are a few small places that are worth paying attention to, otherwise, they will feel puzzled.
First, the Gmail account that is used to send email needs to turn on the pop feature.
Second, pay attention to Gmail's use of the port number, there are 465 and 5,872 (there are no other, I did not go to the careful investigation, if omitted, please Haihan). According to Gmail's documentation, I started with Port 465, always timed out, and the message was not sent successfully. However, when I use Outlook, I also use port 465 to send and receive messages successfully. That's confusing me. After a round of wandering, get a new port number, 587. In, we use port 587 to send mail successfully. I don't understand why Gmail's documentation only mentions 465 and ignores 587.

I hope it will 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: 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.