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 3040
悲&欢浪女
悲&欢浪女 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)

提交回复
热议问题