简体   繁体   中英

Can't send email from code using Gmail smtp

I'm trying to send an email using the Gmail smtp server. When I try to send I get

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

I set the credentials in the code so I don't know what I should add. Is there some other property I have to set?

Code:

MailMessage msg = new MailMessage("noreply@mail.com", "receiver@gmail.com", subject, template);
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("Username", "Password");
client.DeliveryFormat = SmtpDeliveryFormat.International;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;

if you are doing everything correctly, this link is probably what you need. Unlike other mail servers, gmail requires more security when third party applications are considered.You should login to your gmail account and allow third party applications to be able to send and receive mails over gmail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM