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

4

I'm having the following error when working with SMTP and MailMessage in C #:

  

The SMTP server requires a secure connection or the client was not authenticated. The response from the server was: 5.5.1 Requested Authentication Learn more at

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 understood the error message. I've tried changing e-mail and senha , this is my first time using SMTP . I know it's not difficult, just a lack of attention from me.

I've been looking for some helps elsewhere, but I did not find anything to help me, I hope you can help me.

    
asked by anonymous 03.10.2015 / 02:06

2 answers

7

To use Gmail, UseDefaultCredentials must be false . This property must be set before credentials, like this:

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

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

    
03.10.2015 / 02:31
0

If you prefer not to allow access to 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 the "Login and security" option
  • Enable "2-step verification" (follow the requested procedures)
  • Select the "App Passwords" option (follow the requested procedures)
  • Select the "Select app" option
  • Choose the "Other (custom name)" option
  • Enter the name of your site / webapp
  • Copy the password generated by google
  • 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 is done, your webapp or website will be able to connect to the account without being blocked by google security and without reducing the level of security.

    
11.01.2017 / 01:04