0

This is my code

private void sendEmail()
    {
            var fromAddress = new MailAddress("gmail email", "name");
            var toAddress = new MailAddress("email", "name");
            const string fromPassword = "password";
            const string subject = "ERROR";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
                UseDefaultCredentials = false,
                Timeout = 20000
            };
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = "this is an example",
            })
            {
                smtp.Send(message);
            }
    }

When I debug the application, it will stop at the smtp.Send(message) and display an error message of " The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required? "

What should I do? Sometimes it works, sometimes it doesn't.

  • "Sometimes it works, sometimes it doesn't" what do you mean by this? In which scenario it works? In which it fails? – Dnyanesh Sep 15 '14 at 02:41
  • @Dnyanesh Scenario is all the same. When I try running it 5 mins before this, it works. But after that, it starts showing that error. – TPStudent Sep 18 '14 at 02:10
  • In that case your session may be of 5 mins. Please check session timeout value. – Dnyanesh Sep 18 '14 at 02:13

1 Answers1

0

There are multiple possibility in which you can face this issue. Check following things

1) Recheck if you are giving correct email and password.

2) Check if you have two step validation for your GMail account.

3) At times Gmail stops sending mails so log-in you gmail account and and it might start working.

4) In case you have issue of Location issue like your gmail account and production server have different times zone refer this link.

Access for less secure apps Link.

Hope once of this will help you.

Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60
Dnyanesh
  • 2,265
  • 3
  • 20
  • 17