Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

c# - Sometimes System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated

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,

  1. I have turned on Allow less secure apps.
  2. I have disabled 2-Step Verification.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...