0

I was create sample email sender using c# but it shows an error is:

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

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add("toAddress@gmail.com");
        mail.From = new MailAddress("fromAddress@gmail.com", "Test Mail !", System.Text.Encoding.UTF8);
        mail.Subject = "Test Mail";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "Test message";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("myMail@gmail.com", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.UseDefaultCredentials = true;
        client.Send(mail);

Web config:

<system.net>
    <mailSettings>
      <smtp from="myMail@gmail.com">
        <network host="smtp.gmail.com" password="password"
          port="587" userName="myMail@gmail.com" />
      </smtp>
    </mailSettings>
</system.net>
Reshma
  • 1,370
  • 7
  • 24
  • 38
  • 1
    Enable less secure apps. This is a security issue by google for gmail. Check my answer – Abdul Saleem Mar 16 '15 at 10:27
  • Does your Google account have two-factor authentication enabled? If so, you can generate an unique app password which you would use instead of your normal password. – cbr Mar 16 '15 at 10:27
  • 1
    @Reshma See this article by Google: [**Sign in using App Passwords**](https://support.google.com/accounts/answer/185833?hl=en) – cbr Mar 16 '15 at 10:30
  • possible duplicate of [Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required](http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) – Abdul Saleem Mar 16 '15 at 10:35

2 Answers2

1

If you have two-factor authentication enabled, you will need to generate yourself an App Password for your Google account.

The steps for generating such password are as follows ( [Google page] | [Archived page]):

  1. Visit your App passwords page. You may be asked to sign in to your Google Account.
  2. At the bottom, click Select app and choose the app you’re using.
  3. Click Select device and choose the device you’re using.

  1. Click Generate.
  2. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.

  1. Click Done.

Once you click done, you’ll won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.

Then, in your NetworkCredentials, replace your password with the generated 16-digit passcode.

cbr
  • 12,563
  • 3
  • 38
  • 63
0

Try to send the mail by code and when you receive the error, login to your gmail account and check the inbox after 2 or 3 mins. You will receive a mail like this :

#;
#;

enter image description here

#;
#;

enter image description here

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45