9

I was trying to send emails in my Asp.Net C# application using port 465 with host "smtp.gmail.com" but the whole application hangs (keeps loading). When debugging it's stuck at SmtpClient.Send(msg).

Regardless of the credentials specified, it doesn't throw any exceptions to catch or any timeout errors. I have to close or refresh the page to regain access to the page.

If I put port 587 instead then every thing works fine and the SmtpClient.Send responds and sends the email or returns a proper exception such as operation timeout or failure sending email based on the credentials.

I simplified the code below for demonstration:

var smtp = new SmtpClient
                        {
                            Host = "smtp.gmail.com",
                            Port = 465,
                            EnableSsl = true,
                            Timeout = 200, // or any value it doesn't solve the problem
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential("name@gmail.com", "password")
                        };

var msg = new MailMessage("name@gmail.com", "name@gmail.com", "Any subject", "Any body"); 
smtp.Send(msg); // stuck here
smtp.Dispose(); // never reached
msg.Dispose();

I tried SendAsync and reduced the timeout interval but this did not solve the problem. I think the problem is that smtp.gmail.com was reached but did not respond properly to port 465 and that's why the method was stuck.

I need to avoid such behavior since my application allows dynamic settings of the smtp server details and I don't want the whole application to hang in case of wrong details were entered.

Thank you,

enter image description here

Osama Mortada
  • 171
  • 3
  • 12
  • Weird, I've never had a problem and it looks right. Sounds kind of like this problem http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/99d6812c-9efe-4d0c-94d0-493c688450f7/, but I dont' think he really had a solution there. Can you send outbound on your network using any other application? – devshorts Mar 20 '13 at 21:44
  • `using (var smtp = ...)` and `using (var msg = ...)` – Austin Salonen Mar 20 '13 at 21:49
  • @devshorts I captured the TCP traffic only using Wireshark, is this enough? – Osama Mortada Mar 21 '13 at 08:54
  • @AustinSalonen I tried wrapping it as you suggested using (smtp) { using (msg) { smtp.Send(msg); } } but it did not work, the problem is not related to disposing the objects, it's stuck at the Send command. – Osama Mortada Mar 21 '13 at 08:57
  • I know. That's why it was a comment and not an answer. – Austin Salonen Mar 21 '13 at 14:29
  • set in port=25, it may work – sidhewsar May 29 '14 at 13:32
  • possible duplicate of [SmtpClient Timeout doesn't work](http://stackoverflow.com/questions/10467476/smtpclient-timeout-doesnt-work) – quantdev Jun 07 '14 at 03:04

2 Answers2

3

Found this answer in case anyone gets here

SmtpClient Timeout doesn't work

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = socket.BeginConnect("192.168.1.180", 25, null, null);
// Two second timeout
bool success = result.AsyncWaitHandle.WaitOne(2000, true);
if (!success) {
    socket.Close();
    throw new ApplicationException("Failed to connect server.");
}
Community
  • 1
  • 1
user2391759
  • 280
  • 2
  • 7
-1

Wrap your SmptClient in a using statement, to ensure that Dispose is called.

I'm not sure if this is the cause or your problem or not, but I was having the same issues (sent correctly the 1st time, and often fails afterwards) at this has rectified it for me.

mcmillab
  • 2,752
  • 2
  • 23
  • 37