-1

I have this error when I try to send an email from c#

MailMessage mail = new MailMessage("from@gmail.com", "to@hotmail.com");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("from@gmail.com", "pwd");
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

thomasb
  • 5,209
  • 4
  • 57
  • 83
Islam Gx
  • 87
  • 10

2 Answers2

1

You should remove these two lines

client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
0

Try this code

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("from@gmail.com", "name", System.Text.Encoding.UTF8);
        mail.To.Add("to@gmail.com");
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("from@gmail.com", "password");
        client.EnableSsl = true;
        mail.Subject = "this is a test email.";
        mail.Body = "this is my test email body";
        client.Send(mail);
hdkhardik
  • 662
  • 3
  • 22