0

I have created a Windows application which is used to send emails. i have given credentials. i turned on google/settings/lesssecure apps. Eventhough its not sending. Its showing the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required here is my code.

   MailMessage message = new MailMessage();
            message.From = new MailAddress("Selvacsebe23@gmail.com");
            string[] mailaddress = new string[count];
            int i;
            if (textSubject.Text != string.Empty)
            {
                message.Subject = textSubject.Text;
                if (textBody.Text != string.Empty)
                {
                   message.To="Selvakesavan@gmail.com"
                   message.IsBodyHtml = true;
                    string tmpBody = "Hello " + "," + "<br/> <br/>" + textBody.Text + "<br/> <br/>" + "Thanks and Regardds";
                    message.Body = tmpBody;
                    SmtpClient client = new SmtpClient();
                    client.UseDefaultCredentials = true;
                    client.Host = "smtp.gmail.com";
                    client.Port = 587;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential("selvacsebe23@gmail.com", "mypassword");
                    message.Priority = MailPriority.High;
                    client.EnableSsl = true;            
                    client.Send(message);
                    MessageBox.Show("Mail has sent successfully !!!", "Success !");
                }
                else
                {
                    MessageBox.Show("Please Enter Body of the Message !");
                }
            }
            else
            {
                MessageBox.Show("Please Enter Subject !");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Failure !");
            log.Fatal(ex.Message);
        }
    }
pah
  • 4,700
  • 6
  • 28
  • 37
  • 1
    Possible duplicate of [this](http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) – Berkay Yaylacı Jul 23 '16 at 10:30
  • You cannot do that **message.To="Selvakesavan@gmail.com"** because "To" property is a readonly MailAddressCollection of emails. Try **message.To.Add("Selvakesavan@gmail.com");** – derloopkat Jul 23 '16 at 11:03

1 Answers1

1

If you turn on 2-step-verification, then you need to login using app-specific-password. You can create it here: https://support.google.com/accounts/answer/185833?hl=en. If you use your normal password, then you will get exception : 5.5.1 Authentication Required. You don't need lots of code, this code is enough to send email without attachment:

  const string from = "user1@gmail.com";
  const string to = "user2@yahoo.com";
  const string subject = "This is subject";
  const string body = "This is body";
  const string appSpecificPassword = "akdfkajsdhklakdfh";

  var mailMessage = new MailMessage(from, to, subject, body);
  using (var smtpClient = new SmtpClient("smtp.gmail.com", 587))
  {
    smtpClient.EnableSsl = true;
    smtpClient.Credentials = new NetworkCredential(from, appSpecificPassword);
    smtpClient.Send(mailMessage);
  }