简体   繁体   中英

sending the email using gmail smtp - unable to connect the remote server

I tried to send mail using Windows application in C#. I got code as below and tried but it is not working

It giving following error:

Unable to connect the remove server

Code:

MailAddress sendd = new  MailAddress("xxxxxx@gmail.com","Sethu",Encoding.UTF8);
MailAddress receivee = new MailAddress("xxxxxx@yahoo.com");
MailMessage mail = new MailMessage(sendd, receivee);
SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxxxxx@gmail.com", "yyyyyyy");
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);

Anyone help to find out what mistake i did?

can you try make your code to something like the following

 var smptClient = new SmtpClient("smtp.gmail.com", 587)
 {
     Credentials = new NetworkCredential("smit123@gmail.com", "123456789"),
     EnableSsl = true
 };
  smptClient.Send("smit123@gmail.com", "test@gmail.com", "Testing Email", "testing the email");

This code is properly tested with my own account on the Network..

Following code works for me

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("xxxxxx@gmail.com", "yyyyyyy"),
    EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "this is a test email.", "this is my test email body");

which is pretty much the same as yours with the difference that it doesn't use MailMessage object. This might not be the problem, but you could give it a try.

I would also check if you have port 587 enable on your server

You should also go through this topic where you find lots of useful tips

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM