ItGo.me Focus on IT Recommend

Home > c# - Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

c# - Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

2020腾讯云8月秒杀活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

I am using following code to send email. The Code works correctly in my local Machine. But on Production server i am getting the error message

        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);

And on my Gmail A/c I have received the following email after i ran the code from production server

Hi ,

Someone recently used your password to try to sign in to your Google Account mymailid@gmail.com. This person was using an application such as an email, client or mobile device.

We prevented the sign-in attempt in case this was a hijacker trying to access your account. Please review the details of the sign-in attempt:

Friday, 3 January 2014 13:56:08 o'clock UTC IP Address: xxx.xx.xx.xxx (abcd.net.) Location: Philadelphia PA, Philadelphia, PA, USA

If you do not recognise this sign-in attempt, someone else might be trying to access your account. You should sign in to your account and reset your password immediately.

Reset password

If this was you and you are having trouble accessing your account, complete the troubleshooting steps listed at http://support.google.com/mail?p=client_login

Yours sincerely, The Google Accounts team

c# .net smtp gmail
|
  this question
edited Dec 28 '14 at 16:23 GEOCHET 16.8k 15 61 86 asked Jan 3 '14 at 14:40 vcs 864 3 9 15      do you have the access of production server –  Arshad Jan 3 '14 at 14:54      I dont have access to the production server –  vcs Jan 3 '14 at 14:56      Turn off 2-step verification.. –  Mangesh Jan 18 '15 at 5:41      Please, look at stackoverflow.com/questions/34851484/… –  user5515846 Jan 20 '16 at 8:27      Also, make sure the google "Captcha" is disabled - this may be necessary if you are running the script on a remote server (not necessary when running on local machine): accounts.google.com/DisplayUnlockCaptcha –  Jens May 17 '16 at 16:17

 | 

16 Answers
16

---Accepted---Accepted---Accepted---

This generally happens when you try login from different time zone or IP Address Computer. Your production server and the mail id you have used both are in different time zone. Choose any of the one solution:

1) Log in to production server via remote access, and sign in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.

Or 2) log in gmail to your local computer, Follow this Link and choose review this activity and take proper actions.


|
  this answer
edited Aug 11 '16 at 13:50 HEGDE 33 10 answered Jan 3 '14 at 15:06 Arshad 5,758 2 16 48 9   A little comment: go to security.google.com/settings/security/activity and here you'l see blocked connection from your code. Allow them and it should help in this case. –  Alex Zhukovskiy Oct 2 '14 at 12:45 31   This will also help a lot: google.com/settings/security/lesssecureapps –  George Oct 12 '14 at 2:23      Yes, the reason is 2 steps verification switched On for gmail account. Select App/Device and click Generate button support.google.com/accounts/answer/185833 –  smily Dec 25 '15 at 15:34      Look at stackoverflow.com/questions/34851484/… –  user5515846 Jan 20 '16 at 8:27 1   Also, make sure the google "Captcha" is disabled - this may be necessary if you are running the script on a remote server (not necessary when running on local machine): accounts.google.com/DisplayUnlockCaptcha –  Jens May 17 '16 at 16:18

 | 

When you try to send mail from code and you find the error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required", than the error might occur due to following cases.

case 1: when the password is wrong

case 2: when you try to login from some App

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)

There is a solution for each

solution for case 1: Enter the correct 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 . So that you will be able to login from all apps.

solution 2 for case 2:(see http://stackoverflow.com/a/9572958/52277) enable two-factor authentication (aka two-step verification) , and then generate an application-specific password. Use that newly generated password to authenticate via SMTP.

solution 1 for case 3: (This might be helpful) you need to review the activity. but reviewing the activity will not be helpful due to latest security standards the link will not be useful. So try the below case.

solution 2 for case 3: If you have hosted your code somewhere on production server and if you have access to the production server, than take remote desktop connection to the production server and try to login once from the browser of the production server. This will add excpetioon for login to google and you will be allowed to login from code.

But what if you don't have access to the production server. try the solution 3

solution 3 for case 3: You have to enable login from other timezone / ip for your google account.

to do this follow the link https://g.co/allowaccess and allow access by clicking the continue button.

And that's it. Here you go. Now you will be able to login from any of the computer and by any means of app to your google account.


|
  this answer
edited Jun 5 '16 at 13:27 Michael Freidgeim 7,530 4 52 64 answered Nov 3 '14 at 7:39 Roshan Parmar 1,523 1 5 2 4   Solution 2! You little beauty! –  George Harnwell Apr 22 '15 at 14:55 2   lesssecureapps link above helped :) –  Gautam Jain Oct 30 '15 at 12:27      Solution 2 worked perfect charm! –  Venkat Nov 15 '15 at 10:19 1   YOU ARE THE MAN!!!! Worked for me: solution 3 for case 3 –  Thomas Dec 22 '15 at 15:37 1   site hosted on azure need to follow solution 3 for case 3: final link which help g.co/allowaccess –  Ravi Anand Jan 21 '16 at 14:14  |  show more comments

Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.

After Logging in to your e-mail, CLICK HERE

This will take you to the following page

Don't forget to click the back arrow near the title Less Secure Apps after Turning ON.

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

eliveryMethod.Network; client.EnableSsl = true; client.UseDefaultCredentials = true; client.Send(msg); I am running it on localhost so what mistake i am doing to send it. When i send button it gives an error like The SMTP serv


|
  this answer
edited Nov 30 '16 at 7:58 answered Jan 12 '15 at 10:36 Sayka 3,288 1 17 25 1   Sayka is today's hero! Many thanks. –  Bonus Kun Apr 4 '16 at 14:09 2   Thanks for the nice comment @BonusKun –  Sayka Apr 4 '16 at 22:50      It works for me as well, Thanks @Sakya ! –  Rahul Hendawe Sep 22 '16 at 5:25      Also, if you have a custom domain, users first need to be enabled by domain administrator to allow access to less secure apps. If you are a domain admin, go to the following link to enable this admin.google.com/AdminHome#ServiceSettings/… –  user551113 Feb 20 at 11:51

 | 

Hi I had the same issue,

what I've done to solve it. is to turn on the less secure app. after connecting to my gmail account. I entered this link: https://www.google.com/settings/security/lesssecureapps

Then I turn on the secure app and, and the it worked. it has been said also above


|
  this answer
answered Mar 5 '15 at 13:18 Barak Rosenfeld 150 2 12

 | 

Just follow the step in the google email and enable less secure apps.


|
  this answer
edited Oct 29 '14 at 21:18 George Stocker ♦ 41.2k 25 139 206 answered Oct 29 '14 at 18:00 Trung 81 1 1

 | 

I have faced the same problem. It happens when you turn on 2 Step Verification (MFA). Just Turn off 2 Step Verification and your problem should be solved.


|
  this answer
edited Jul 31 '14 at 16:43 fortran 41.3k 14 91 138 answered Jul 31 '14 at 16:02 Anik 71 1 1      I did not have two-step enabled for this account and had this issue. –  Shiv Apr 24 '15 at 5:45

 | 

After spending a couple of hours today trying every solution here, I was still unable to get past this exact error. I have used gmail many times in this way so I knew it was something dumb, but nothing I did fixed the problem. I finally stumbled across the solution in my case so thought I would
 .

First, most of the answers above are also required, but in my case, it was a simple matter of ordering of the code while creating the SmtpClient class.

In this first code snippet below, notice where the "Credentials = creds" line is located. This implementation will generate the error referenced in this question even if you have everything else set up properly.

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
        }

However, if you move the Credentials setter call to the bottom, the email will be sent without error. I made no changes to the surrounding code...ie...the username/password, etc. Clearly, either the EnableSSL, UseDefaultCredentials, or the DeliveryMethod is dependent on the Credentials being set first....I didn't test all to figure out which one it was though.

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
        }

Hope this helps save someone else some headaches in the future.


|
  this answer
answered Dec 11 '15 at 20:54 user1011627 223 3 13      In my case, I had the same problem as yours, reorderring the code so that Credentials came last worked for me. –  Dennis Mar 24 '16 at 8:55      Thanks so much! –  kipusoep Jan 15 at 14:44      I can not believe it but it worked! I have enabled 2-step, because did not want to enable less secure flag, but till I put Credential last before Send it did not want to work. Incredible! –  Čikić Nenad Feb 6 at 14:47

 | 

Below is my code.I also had the same error but the problem was that i gave my password wrong.The below code will work perfectly..try it

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");             
            mail.From = new MailAddress("fromaddress@gmail.com");
            mail.To.Add("toaddress1@gmail.com");
            mail.To.Add("toaddress2@gmail.com");
            mail.Subject = "Password Recovery ";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";
            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";
            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";
            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

You can refer it in my blog


|
  this answer
edited May 13 '14 at 2:34 answered May 13 '14 at 2:12 Ganesh Pillai N 51 4

 | 

What worked for me was to activate the option for less secure apps (I am using VB.NET)

Public Shared Sub enviaDB(ByRef body As String, ByRef file_location As String)
        Dim mail As New MailMessage()
        Dim SmtpServer As New SmtpClient("smtp.gmail.com")
        mail.From = New MailAddress("from@gmail.com")
        mail.[To].Add("to@gmail.com")
        mail.Subject = "subject"
        mail.Body = body
        Dim attachment As System.Net.Mail.Attachment
        attachment = New System.Net.Mail.Attachment(file_location)
        mail.Attachments.Add(attachment)
        SmtpServer.Port = 587
        SmtpServer.Credentials = New System.Net.NetworkCredential("user", "password")
        SmtpServer.EnableSsl = True
        SmtpServer.Send(mail)
    End Sub

So log in to your account and then go to google.com/settings/security/lesssecureapps


|
  this answer
answered Dec 2 '15 at 21:39 Edgar 305 1 8 1   The link is dead. –  Zero3 Mar 18 '16 at 11:37      google.com/settings/security/lesssecureapps –  kipusoep Jan 15 at 14:43

 | 

I have really looked at a lot of ideas, the only solution was this way (works with different email Providers):

            try
        {
            ViewProgressbar("Try to connect mail-server...", progressBar1.Value = 20);
            string host = dsProvider.Rows[y]["POP_hostOut"].ToString();
            int port = int.Parse(dsProvider.Rows[y]["POP_portOut"].ToString());  //587
            string[] email = von1.Split('@');
            string userName = (dsProvider.Rows[y]["login"].ToString() == "email[0]@email[1]")? email[0]+"@"+email[1] : email[0];
            string password = layer.getUserPassword(listSender.SelectedValue.ToString());
            SmtpClient client = new SmtpClient(host, port);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            //A idea from MSDN but it not works. You got the "The server response was: 5.5.1 Authentication Required."
            //System.Net.NetworkCredential myCreds = new System.Net.NetworkCredential(userName, password, host);
            //System.Net.CredentialCache cache = new System.Net.CredentialCache();
            //cache.Add(host, port, "NTLM", myCreds);
            ///cache.GetCredential(host, port, "NTLM");   //NTLM
            client.Credentials = new System.Net.NetworkCredential(userName, password);
            client.Host = host;
            client.Port = port;
            client.EnableSsl = true;
            client.Send(message);
            ViewProgressbar();
        }
        catch (SmtpException ex)...

|
  this answer
answered May 9 '14 at 11:21 Walter 11 2

 | 

I had the same problem for an application deployed to Microsoft Azure.

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

First I approved all unknown devices (some ip-addresses originating from Ireland) on the following page (signed in as the gmail user): https://security.google.com/settings/u/1/security/secureaccount

I used the following settings for the client:

var client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("my_user_name@gmail.com", "my_password"); 

It started working only after I set the following property on the smtp-client:

client.TargetName = "STARTTLS/smtp.gmail.com";

|
  this answer
answered Sep 11 '15 at 12:38 haiiaaa 113 1 6

 | 

I'm a google apps for business subscriber and I spend the last couple hours just dealing with this, even after having all the correct settings (smtp, port, enableSSL, etc). Here's what worked for me and the web sites that were throwing the 5.5.1 error when trying to send an email:

  1. Login to your admin.google.com
  2. Click SECURITY <-- if this isn't visible, then click 'MORE CONTROLS', and add it from the list
  3. Click Basic Settings
  4. Scroll to the bottom of the Basic Settings box, click the link: 'Go to settings for less secure apps'
  5. Select the option #3 : Enforce access to less secure apps for all users (Not Recommended)
  6. Press SAVE at the bottom of the window

After doing this my email forms from the website were working again. Good luck!


|
  this answer
edited Nov 20 '15 at 17:30 answered Nov 20 '15 at 17:08 RichieMN 256 1 4 16

 | 

You might need to create/generate a specific APP password from gmail. you app or script will then use this new password instead of your regular password. Your regular password will still work fine for you.

That is what did it for me. I still used the same email account but had to generate a new app specific password.

https://support.google.com/accounts/answer/185833?hl=en

Basically you can do it here: https://security.google.com/settings/security/apppasswords


|
  this answer
edited Jan 20 '16 at 20:38 answered Jan 20 '16 at 20:33 samiup 106 1 4

 | 

just turn on the setting of or gmail. see below given image:


|
  this answer
answered 2 hours ago Rajesh Paliwal 56 3

 |  -1

I used all of the above mentioned solutions but it finally worked only after i enabled IMAP Access from Gmail settings Link to Enable IMAP Access in gmail settings

Of course, the points in the other solutions were required too.


|
  this answer
answered Sep 28 '15 at 13:07 AndroidMechanic 10.8k 7 34 72      This didn't help me. Enabling IMAP just enables you to read Gmail email messages from other mail clients like Outlook, Thunderbird. –  kimbaudi Oct 11 '16 at 22:20

 |  -4

try changing the host, this is the new one, I got this configuring mozilla thunderbird

Host = "smtp.googlemail.com"

that work for me


|
  this answer
edited Dec 8 '15 at 9:10 w͏̢in̡͢g͘̕ed̨p̢͟a͞n͏͏t̡͜͝he̸r̴ 7,837 3 15 46 answered Dec 15 '14 at 16:40 patogalarzar 11

 | 

protected by Community♦ Feb 26 '15 at 9:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged c# .net smtp gmail or ask your own question.

Recommend:c# - Error. The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

nnection or the client was not authenticated. The server response was: 5.5.1 Authentication Required 16 answers This is my code private void sendEmail() { var fromAddress = new MailAddress("gmail email", "name");

oriUlr:http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not

------splitte line----------------------------