The SMTP server requires a secure connection or the client was not authenticated

Exception


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

Explanation


The reason why you are getting the above exception while trying to send an email from your code by using Gmail SMTP, is because Google is blocking you from signing in to your account due to security reasons.

Solution


In order to handle this exception go to Less secure apps section of your Google account and turn on Allow less secure apps.

Allow less secure apps