2

I am getting the following error

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

How to resolve this? My code:

MailMessage mail = new MailMessage();
mail.From = new MailAddress(FromAddress);
mail.To.Add(email);
mail.Subject = "Forgot Password";
mail.Body = SendMessage;
mail.IsBodyHtml = true;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
SmtpServer.Port = 587;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential myCreds = new NetworkCredential(EmailUsername, EmailPassword);
SmtpServer.Credentials = myCreds;
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = true;
SmtpServer.Send(mail);

Webconfig as follows

<add key="FromAddress" value="[email protected]"/>
<add key="EmailUsername" value="[email protected]"/>
<add key="EmailPassword" value="Josco@321"/>

Changed my email settings as follows.

enter image description here

FromAddress and EmailUsername are same email.

2
  • Not directly relevant to your question, but MailMessage and SmtpClient both implement IDisposable, and in case of SmtpClient in particular, you can actually exhaust resources if you don't dispose. Commented Aug 8, 2017 at 18:56
  • Be sure to change your password as it is showing in the question ;-) Commented Aug 8, 2017 at 19:05

1 Answer 1

4

Ensure you set SmtpClient.Credentials AFTER calling SmtpClient.UseDefaultCredentials = false.

The order is important as setting SmtpClient.UseDefaultCredentials = false will reset SmtpClient.Credentials to null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.