Solve the problem that gmail.com mailbox is used in. Net to send SMTP mails.

Source: Internet
Author: User
Tags mailmessage smtpclient
I often use a free gmail.com mailbox because it has a large capacity, but we often encounter unexpected errors when using. NET programming to send emails. The most common is:

(1) The operation has timed out.
(2) A similar prompt is displayed:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 must issue a starttls command first ...."

The main cause of this error is:
(1) The pop settings are incorrect;
(2) This is caused by incorrect usedefaultcredentials, credentials, and enablessl settings of smtpclient.

First, you need to go to the gmail.com mailbox and run"Forwarding and POP/IMAP". For example:

Then, set the following items in your SMTP program:
// Create an smtpclient to send an email
Smtpclient client = new smtpclient ();

Mailmessage message = new mailmessage ();

// Set the sender's email address
Message. From = new mailaddress (fromaddress );

// Set the recipient's email address
Message. to. Add (toaddress );
// Set the reply email address
Message. replyto = new mailaddress (replytoaddress );

// Set the CC email address
// Message. CC. Add (ccaddress );
// Message. bcc. Add (bccaddress );

// Set the mail subject and content
Message. Subject = msgsubject;
Message. Body = body;
Message. isbodyhtml = ishtmlbody;
// Set the SMTP host and Port
Client. Host = "smtp.gmail.com ";
Client. Port = 25;
Client. usedefacrecredentials = false;
System. net. networkcredential basicauthenticationinfo = new system. net. networkcredential (fromaddress,Smtppassword);
Client. Credentials = basicauthenticationinfo;
Client. enablessl = true;
Client. Send (Message );

I wish you a happy life!

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.