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

Asked

Viewed 24,321 times

5

I’m having the following mistake, working with SMTP and MailMessage in the C#:

The SMTP server requires a secure connection or the client has not been authenticated. The server response was: 5.5.1 Requested authentication Learn more on

Or in English

The SMTP server requires a Secure Connection or the client was not authenticated. The server Response was: 5.5.1 Authentication Required. Learn more at

    /// <summary>
    /// Servidor de E-mail
    /// </summary>
    protected SmtpClient SmtpClient { get; set; }

    /// <summary>
    /// Conteudo da Mensagem
    /// </summary>
    protected MailMessage MailMessage { get; set; }
    #endregion 

   /// <summary>
   /// Método enviar e-mail
   /// </summary>
   /// <param name="smtp"></param>
   /// <param name="from"></param>
   /// <param name="to"></param>
   /// <param name="subject"></param>
   /// <param name="body"></param>
   /// <param name="priority"></param>
    public string EnviarEmail(string smtp, string from, string to, string subject, string body, bool priority)
    {
        try
        {
            SmtpClient = new SmtpClient();
            SmtpClient.Host = "smtp.gmail.com";
            SmtpClient.Port = 587;
            SmtpClient.EnableSsl = true;
            SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpClient.Credentials = new NetworkCredential("[email protected]","senha");
            SmtpClient.UseDefaultCredentials = true;

            MailMessage = new MailMessage();
            MailMessage.From = new MailAddress(from, "Raffa Ferreira", Encoding.UTF8);
            MailMessage.To.Add(new MailAddress(to, "Fulano teste", Encoding.UTF8));

            MailMessage.Subject = subject;
            MailMessage.Body = body;
            MailMessage.BodyEncoding = Encoding.UTF8;
            MailMessage.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");

            if (priority == false)
            {
                MailMessage.Priority = MailPriority.Normal;
            }
            else
            {
                MailMessage.Priority = MailPriority.High;
            }

            SmtpClient.Send(MailMessage);
        }
        catch(SmtpFailedRecipientException ex)
        {
            Console.WriteLine("Mensagem : {0} " + ex.Message);
        }
        catch(SmtpException ex)
        {
            Console.WriteLine("Mensagem SMPT Fail : {0} " + ex.Message);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Mensagem Exception : {0} " + ex.Message);
        }

        string mensagem = "E-mail enviado";
        return mensagem;
    }

What I’m doing wrong, I got the message of the mistake. I tried to change the e-mail and the senha, is the first time I’ve used SMTP I’m new at this. I know it’s not hard, just a lack of attention from me.

For now I’ve looked for some helps in other places, but I couldn’t find anything to help me, I hope you can help me.

  • You can do log in on Gmail on the same machine where you’re running this? I hope you haven’t posted your password for everyone to see.

  • I didn’t post no, where is "password" is something fictitious. I just logged in with the same email and password, and logged in normally. But during the execution of the project, he brings me this failure.

  • When you loga on Gmail, does it ask for access confirmation? Does it indicate unusual usage? See this? https://security.google.com/settings/security/activity Have you tried to change this? https://www.google.com/settings/security/lesssecureapps

  • It shows me some access quests, Devices and PC’s !

2 answers

9


To use Gmail, UseDefaultCredentials must be false. This property must be defined before of credentials, thus:

SmtpClient.UseDefaultCredentials = false;
SmtpClient.Credentials = new NetworkCredential("[email protected]","senha");

You should also allow "less secure" access to your Gmail, through the page Less secure apps.

  • Okay, thank you very much !

  • @Raffaferreira or make your application use XOAUTH, so you don’t need to authorize less secure applications.

  • "Less secure apps.".. That’s what was wrong with me.rs

  • After a lot of running the Internet, finally that was the cause. It really helped a lot!

  • I had the same problem and the solution was in gmail itself in authorizing applications. Thank you very much.

0

If you prefer not to allow access from less secure applications, the following solution can be adopted:

  • Log in to the gmail account that will be used by the site select "My Account"
  • Select "Login and security"
  • Activate "Two-Step Verification" (follow the requested procedures)
  • Select the "App passwords" option (follow the requested procedures)
  • Select the option "Select app"
  • Choose "Other (custom name)"
  • Enter the name of your website / webapp
  • Copy the google generated password
  • In the code snippet below, enter the generated password instead of the account password:

    Smtpclient.Credentials = new Networkcredential("[email protected]","password generated by google");

This done, your webapp or website will be able to connect to the account without being barred by google security and without reducing the security level.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.