The code below is throwing an exception:
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
var fromAddress = new MailAddress(toEmail, "From Name");
var toAddress = new MailAddress(fromEmail, "To Name");
const string fromPassword = "******";
string subject = sub;
try
{
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword) };
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}