0

What code should I use to send email in C#?

I tried to find a specific code so that I could send an email from my website. And then I get an error: "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"

public void sendEmail(string toEmail, string subject, string emailBody)
{
     string senderEmail = "My_Email";
     string senderPassword = "My_Email_Password";
     SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
     client.EnableSsl = true;
     client.Timeout = 500000;
     client.DeliveryMethod = SmtpDeliveryMethod.Network;
     client.UseDefaultCredentials = false;
     client.Credentials = new NetworkCredential(senderEmail, senderPassword);
     MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
     mailMessage.IsBodyHtml = true;
     mailMessage.BodyEncoding = UTF8Encoding.UTF8;
     client.Send(mailMessage);
}

Do I need to use Google API??

1 Answers1

0

Its a security issue, Gmail by default prevents access for your e-mail account from custom applications. You can set it up to accept the login from your application.

You need to go to security settings at the followig link https://www.google.com/settings/security/lesssecureapps and enable less secure apps. So that you will be able to login from all apps.

A. Nadjar
  • 2,440
  • 2
  • 19
  • 20
  • You'll certainly find this thread useful: https://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not – A. Nadjar Aug 14 '19 at 05:27