I want to send an email using SMTP[in c#]. And based on what I understood, I wrote such this code.
using System;
using System.Net;
using System.Net.Mail;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start......*******");
SmtpClient c = new SmtpClient()
{
Port = 587,
Host = "smtp.gmail.com",
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential()
{
UserName = "[email protected]",
Password = "myPassword",
}
};
MailAddress TestFrom = new MailAddress("[email protected]");
MailAddress TestTo = new MailAddress("[email protected]");
MailMessage message = new MailMessage()
{
From = TestFrom,
Subject = "Subject test",
Body = "Body test",
};
message.To.Add(TestTo);
c.Send(message);
Console.WriteLine("END*****");
}
}
But it threw an exception and didn’t work…
System.Net.Mail.SmtpException: ‘The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. For more information, go to’
And I realized that I need to turn on the option for “Less Secure app access” in my gmail account settings.
But it seems like such an option has been removed by Gmail. So what is the solution now and how can I fix my code???
Matin Baki is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1