26
votes

While working with Email sending in C#.NET in visual studio 2008 i got the below error

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.53.108:25

But the same code was working fine in some other PC but when i am testing today it gives me error in Send() method... Also my network connection is good where i am testing the email code..

Below is my email code

MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential("[email protected]",
                                                            "MyPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

What could be the reasons for such error..???

2
Maybe you exceeded the limit of sending mails from the account and operations might have been temporarily suspended for the account, did you try with another account ?V4Vendetta
This is the first time i am testing this today but i dont think because of such reason there could be problem also i have send 5 mail previously in 1 day..... I have not tried with other account... but i will try that...DShah
i have tried with other account also but did not work and same error occurs...DShah
Why is the port shown as 25 are you setting it explicitly ? its a different port for SSL, if you are specifying any port just remove thatV4Vendetta
I think you should try this: stackoverflow.com/a/11513412/676508 This solution saved my world.Yoosaf Abdulla

2 Answers

28
votes

The following code works for me. Your code was giving me errors, I believe it was due to not setting the port to 587.

http://forums.asp.net/t/1250771.aspx/4/10

MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(address, password);
smtp.Send(mail);
2
votes

This happened to me due to my company security wifi. Once I changed to open wifi, the problem was solved automatically.