3

I use the following code to send an email from my Gmail account. But the following error is occurred. Unable to connect to the remote server what's wrong with my code?

        string from = "xxxx@gmail.com";
        string to = TxtTo.Text;
        string cc = "yyyy@gmail.com";
        string subject = TxtSubject.Text;
        string body = EdtContext.Content;
        MailMessage EMail = new MailMessage();
        MailAddress Sender = new MailAddress(from);
        EMail.From = Sender;
        EMail.To.Add(to);
        EMail.CC.Add(cc);
        EMail.Body = body;
        EMail.IsBodyHtml = true;
        EMail.Subject = subject;
        EMail.BodyEncoding = Encoding.UTF8;
        SmtpClient Local = new SmtpClient("smtp.gmail.com");//--- smtp
         Local.Port = 465;
         Local.EnableSsl = true;
         Local.UseDefaultCredentials = false;           
        Local.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "*****");//------ email user and pass in sobhan mail
        Local.Send(EMail);

***************
**Port 587 solved problem**
sajadre
  • 1,141
  • 2
  • 15
  • 30
Raymond Morphy
  • 2,464
  • 9
  • 51
  • 93

1 Answers1

0

make sure you have set right credentials for Gmail

Also, I think that the port used to send mail to gmail is 465 for SSL or 587 for TLS/STARTTLS.

similar problem has been discussed on this Link

Sending email in .NET through Gmail

Community
  • 1
  • 1
manishsingh2061
  • 521
  • 1
  • 6
  • 13