29

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("MyUserName@gmail.com",
                                                            "MyPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

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

Jim G.
  • 15,141
  • 22
  • 103
  • 166
DShah
  • 9,768
  • 11
  • 71
  • 127
  • 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 Aug 18 '11 at 05:39
  • 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 Aug 18 '11 at 06:10
  • i have tried with other account also but did not work and same error occurs... – DShah Aug 18 '11 at 07:09
  • 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 that – V4Vendetta Aug 18 '11 at 07:13
  • I think you should try this: http://stackoverflow.com/a/11513412/676508 This solution saved my world. – Yoosaf Abdulla Oct 03 '12 at 18:41

3 Answers3

33

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);
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
mutek
  • 423
  • 5
  • 8
5

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

SarahX
  • 61
  • 2
  • 6
0

This was a firewall issue, adding a rule to bypass your IP / IP of application server resolves this and is able to connect to the SMTP server.

Zedverse07
  • 71
  • 6