//less security is enabled
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("pk.uzikhan@gmail.com", "*********");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
// ViewBag.Message = "Email sent.";
return Ok("email sent");
Asked
Active
Viewed 9,841 times
2

Lasse V. Karlsen
- 380,855
- 102
- 628
- 825

uzair khan
- 33
- 1
- 4
-
`smtp.UseDefaultCredentials = false` FYI, Google seems to make a breaking change to gmail every few months. – rfmodulator Mar 28 '22 at 19:03
1 Answers
0
I think you could read the official document about SmtpClient:https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.credentials?view=netframework-4.8#system-net-mail-smtpclient-credentials
you could see the Remark on the "SmtpClient.Credentials Property":
To use your default network credentials, you can set the UseDefaultCredentials to true instead of setting this property.
Make sure you are using the correct account and password.Also, don't forget set your google account as below:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress(emailaddress);
mail.To.Add(emailaddress);
mail.Subject = "Hi";
mail.Body = "Doc";
SmtpServer.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential(emailaddress, password);
SmtpServer.Credentials = NetworkCred;
SmtpServer.EnableSsl = true;
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.gmail.com";
SmtpServer.Send(mail);
You could see the explain of the error message in this case: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated

Ruikai Feng
- 6,823
- 1
- 2
- 11
-
1
-
It will no longer work as less secure app feature is now removed by Google. It's also clearly visible in the image shared by responder. – Utkarsh Dubey Sep 21 '22 at 09:27
-
1This will be valid solution after May 30, 2022. [App Password](https://stackoverflow.com/a/66169647/6845412) – Utkarsh Dubey Sep 21 '22 at 11:26