-1

This error shows up when I try to send mail

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

Here is my code:

   MailMessage mail = new MailMessage();
   SmtpClient SmtpServer = new SmtpClient(MySMTPClient);

   mail.From = new MailAddress(MySendingAddress);
   mail.To.Add(recieverAddress);
   mail.Subject = subject;
   mail.Body = content;

   SmtpServer.Port = MyPort;
   SmtpServer.Credentials = new System.Net.NetworkCredential(MySendingAddress, MyPassword);//(MySendingAddress, MyPassword);
   SmtpServer.EnableSsl = true;
   SmtpServer.UseDefaultCredentials = false;
   SmtpServer.Send(mail);
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Try this code to mail :

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Username);
mail.To.Add(emailTo);
mail.Subject = Subject;
mail.Body = Body;

SmtpServer.Port = 587; // Also Add the port number to send it, its default for Gmail
SmtpServer.Credentials = new System.Net.NetworkCredential(Username, Password);
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 20000; // Add Timeout property, it is optional part
SmtpServer.Send(mail);

let me know if it works.

Krunal Patil
  • 3,666
  • 5
  • 22
  • 28