2
 //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");
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
uzair khan
  • 33
  • 1
  • 4

1 Answers1

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:

enter image description here enter image description here I tested with the codes:

            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);
            

The result: result

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