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

后端 未结 25 2450
悲&欢浪女
悲&欢浪女 2020-11-22 02:12

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 fromAddres         


        
相关标签:
25条回答
  • 2020-11-22 03:07

    I have a previously-working code that throws this error now. No issue on password. No need to convert message to base64 either. Turns out, i need to do the following:

    1. Turn off 2-factor authentication
    2. Set "Allow less secure apps" to ON
    3. Login to your gmail account from production server
    4. Go here as well to approve the login activity
    5. Run your app in production server

    Working code

        public static void SendEmail(string emailTo, string subject, string body)
        {
            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("youremail@gmail.com", "secretpassword"),
                EnableSsl = true
            };
    
            client.Send("youremail@gmail.com", emailTo, subject, body);
        }
    

    Turning off 2-factor authentication

    Set "Allow less secure apps" to ON (same page, need to scroll to bottom)

    0 讨论(0)
  • 2020-11-22 03:08

    You need to turn on "Allow less secure apps" from here https://myaccount.google.com/lesssecureapps

    0 讨论(0)
  • 2020-11-22 03:08

    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 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)...
    
    0 讨论(0)
  • 2020-11-22 03:08

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

    Host = "smtp.googlemail.com"
    

    that work for me

    0 讨论(0)
  • 2020-11-22 03:10

    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 https://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.

    0 讨论(0)
  • 2020-11-22 03:10

    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

    0 讨论(0)
提交回复
热议问题