Coder Perfect

The SMTP server requires a secure connection, or the client was not authorized, according to Gmail. 5.5.1 Authentication Required was the server response.

Problem

To send email, I use the following code. In my local machine, the code runs fine. On the other hand, I’m getting an error message on the Production server.

var fromAddress = new MailAddress("mymailid@gmail.com");
var fromPassword = "xxxxxx";
var toAddress = new MailAddress("yourmailid@yourdoamain.com");

string subject = "subject";
string body = "body";

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)       
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})

smtp.Send(message);

After running the code from the production server, I received the following email on my Gmail account.

Asked by vcs

Solution #1

When attempting to send email from code and encountering an error “If the client is not authenticated, the SMTP server requires a secure connection. 5.5.1 Authentication Required was the server response “, then the issue could be caused by one of the following scenarios.

The first scenario is when the password is incorrect.

instance 2: when you try to log in from a third-party application

case 3: when you try to login from the domain other than your time zone/domain/computer (This is the case in most of scenarios when sending mail from code)

Each problem has a solution.

Case 1 solution: Enter the proper password.

solution 1 for case 2: go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps . As a result, you’ll be able to log in from any app.

(See https://stackoverflow.com/a/9572958/52277 for solution 2) Activate two-factor authentication (also known as two-step verification) and create an app-specific password. Authenticate via SMTP using the newly issued password.

Solution 1 for Case 3: You should revisit the activity (this may be useful). However, owing to the most recent security rules, reviewing the activity will be ineffective. So, take a look at the example below.

Solution 2 for Case 3: If your code is hosted on a production server and you have access to it, connect to it through remote desktop and try to log in once from the production server’s browser. This will add an exception for logging into Google, allowing you to log in using code.

But what if you don’t have access to the server in production? Solution 3 should be tried.

Solution 3 for Case 3: For your Google account, you must enable login from various timezones/ip addresses.

To do so, go to https://g.co/allowaccess and click the proceed button to provide access.

That’s all there is to it. So there you have it. You will now be able to access your Google account from any computer and through any app.

Answered by Roshan Parmar

Solution #2

This usually happens when you try to log in from a different time zone or computer with a different IP address. The time zones of your production server and the email address you used are both different. Choose one of the following two options:

1) Log in to the production server using remote access, and then sign in to gmail using your credentials. They’ll request confirmation, confirm it, and then log out.

Alternatively, you can log in to Gmail on your local computer, click this link, and select Review this Activity and Take Appropriate Actions.

Answered by Arshad

Solution #3

It’s a matter of safety. Gmail blocks bespoke programs from accessing your e-mail account by default. You can configure it to accept logins from your app.

CLICK HERE after you’ve logged in to your e-mail account.

This will direct you to the next page.

Answered by Abdul Saleem

Solution #4

I tried every option here for a couple of hours today and was still unable to get past this specific problem. I’d used gmail in this manner before, so I knew it was something stupid, but nothing I tried fixed the issue. I have found the solution to my problem and thought I’d share it.

First, while most of the solutions above are required, in my instance, it was only a matter of rearranging the code while constructing the SmtpClient class.

Notice where the Credentials = creds line appears in the first code snippet below. Even if you have everything else set up correctly, this implementation will produce the problem mentioned in this question.

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
    Host = Emailer.Host,
    Port = Emailer.Port,
    Credentials = creds,
    EnableSsl = Emailer.RequireSSL,
    UseDefaultCredentials = false,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
}

The email will be sent without error if you move the Credentials setting call to the bottom. I didn’t make any modifications to the surrounding code, such as the username and password. Clearly, the Credentials must be configured before EnableSSL, UseDefaultCredentials, or the DeliveryMethod can be used… But I didn’t test them all to see which one it was.

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
{
    Host = Emailer.Host,
    Port = Emailer.Port,
    EnableSsl = Emailer.RequireSSL,
    UseDefaultCredentials = false,
    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network,
    Credentials = creds
}

I hope this saves someone else some time and aggravation in the future.

Answered by user1011627

Solution #5

From here, https://myaccount.google.com/lesssecureapps, you must enable “Allow less secure apps.”

Answered by Raj K

Post is based on https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not