-1

I'm working in windows application and C#, I am using following code to send email. The Code not works correctly in my system:

MailMessage mailmsg = new MailMessage();
SmtpClient smtpclient = new SmtpClient();
mailmsg.To.Add(txtTo.Text);
mailmsg.CC.Add(txtCC.Text);
mailmsg.Subject = txtSubj.Text;
mailmsg.From = new MailAddress("buvana@gmail.com");
mailmsg.Body = txtbody.Text;

smtpclient.Port = 587;
smtpclient.Host = "smtp.gmail.com";
smtpclient.EnableSsl = true;
smtpclient.UseDefaultCredentials = false;
smtpclient.Credentials = new NetworkCredential("buvana@gmail.com", "*********");
smtpclient.Send(mailmsg);

How to solve this problem.

Received this error:

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

The server response was: 5.5.1 Authentication Required

Community
  • 1
  • 1

1 Answers1

0

Your code seems to be fine, useDefaultCredentials is false, port is 587, etc.

I reckon the problem is that you need to configure Gmail to allow Less Secure Applications, following these instructions: https://support.google.com/accounts/answer/6010255?hl=en

Go to the "Less secure apps" section in My Account.

Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)

If you are using 2 factor authentication, you'll need to create a new app password for your application and use that to log in.

Community
  • 1
  • 1
user1666620
  • 4,800
  • 18
  • 27