I use SmtpClient to send mail. However, when I run the code, sometimes it throws "System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required."
If I run the code 10 times, it is possible that there is 1 times failed to send and throws the exception "System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required."
Below is my code:
using (SmtpClient smtpClient = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("*************", "**********")
})
{
MailMessage mail = new MailMessage
{
From = new MailAddress("*************@gmail.com", "******")
};
mail.To.Add("**********@gmail.com");
mail.Subject = "Password Reset";
mail.IsBodyHtml = true;
mail.Body = "test";
smtpClient.TargetName = "SMTPSVC/smtp.gmail.com";
smtpClient.Send(mail);
};
For your information,
- I have turned on Allow less secure apps.
- I have disabled 2-Step Verification.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…