9
votes

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("[email protected]", "password")
                        };

var msg = new MailMessage("[email protected]", "[email protected]", "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

2
Weird, I've never had a problem and it looks right. Sounds kind of like this problem social.msdn.microsoft.com/Forums/en-US/ncl/thread/…, but I dont' think he really had a solution there. Can you send outbound on your network using any other application? - devshorts
using (var smtp = ...) and using (var msg = ...) - Austin Salonen
@devshorts I captured the TCP traffic only using Wireshark, is this enough? - Osama Mortada
@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
I know. That's why it was a comment and not an answer. - Austin Salonen

2 Answers

3
votes

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.");
}
-1
votes

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.