I am trying to send email through gmail server but is giving exception.The exception thrown is --> SMTP server requires a secure connection or the client was not authenticated.The server response was 5.7.0.Authentication required.
try
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("account1@gmail.com");
mail.To.Add("account2@gmail.com");
mail.Subject = "Hello World";
mail.Body = "<h1>Hello</h1>";
mail.IsBodyHtml = true;
// mail.Attachments.Add(new Attachment("C:\\file.zip"));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("account1@gmail.com", "password");
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}