This is my code
private void sendEmail()
{
var fromAddress = new MailAddress("gmail email", "name");
var toAddress = new MailAddress("email", "name");
const string fromPassword = "password";
const string subject = "ERROR";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
UseDefaultCredentials = false,
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = "this is an example",
})
{
smtp.Send(message);
}
}
When I debug the application, it will stop at the smtp.Send(message) and display an error message of " The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required? "
What should I do? Sometimes it works, sometimes it doesn't.