Reproducing your test - it works for me
You asked if anyone has encountered the same problem - I just tried your code on Windows 7, VS 2008 with .NET 2.0 - it worked just fine. With the timeout set to 1
, as you have it, I get this error almost immediately:
Unhandled Exception: System.Net.Mail.SmtpException: The operation has timed out
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at mailtimeout.Program.Main(String[] args) in c:\test\mailtimeout\Program.cs:line 29
I think the problem may be that you are expecting something different from timeout. Timeout means that the connection was made successfully, but the response did not come back from the server. This means you need to actually have a server listening on port 25 at your destination, but it doesn't respond. For this test, I use Tcl to create a socket on 25 that did nothing:
c:\> tclsh
% socket -server foo 25
When I changed the timout to 15000
, I didn't get the timeout error unti l5s later.
Why Smtp.Timeout has no effect if the connection can't be made
If nothing is listening at port 25, or the host is not reachable, the timeout is not going to happen until at least 20s, when the system.net.tcpclient
layer times out. This is below the system.net.mail
layer. From an excellent article describing the problem and solution:
You will notice that neither of the two classes, System.Net.Sockets.TcpClient nor System.Net.Sockets.Socket has a timeout to connect a socket. I mean a timeout you can set. .NET Sockets do not provide a Connect Timeout when calling the Connect/BeginConnect method while establishing a Synchronous/Asynchronous socket connection. Instead, connect is forced to wait a very long time before an Exception is thrown if the server it tried to connect to is not listening or if there is any network error. The default timeout is 20 - 30 seconds.
There is no ability to change that timeout from mail (which makes sense, mail servers are usually up), and in fact there's no ability to change the connect from system.net.socket
, which is really surprising. But you can do an asynchronous connect, and can then tell if your host is up and the port open. From this MSDN thread, and in particular this post, this code works:
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.");
}