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!