Gonzo345
Gonzo345

Reputation: 1333

Weird timeout behaviour when trying to connect to a hostname which hasn't got any mailserver

I developed a custom class which used MailKit and MimeKit for interacting with an IMAP4 mailserver. It worked like a charm but I noticed recently about something that makes me feel a little uncomfortable:

Looks like if you concrete a host which exists but has no mailserver, it tries to connect for about 30 seconds finally throwing an exception, which is quoted down here. It feels like it's trying to search for a mailserver but as nobody replies as it, it gets kinda "lost".

I setted the Timeout property to 5000ms but looks like it just applies once connected.

The thing is... is there any way to control this in a superior "layer" through .NET itself or should this be considered as a MailKit bug?

Calling to Connect:

imap.ServerCertificateValidationCallback = (s, c, h, e) => true;
imap.Connect(hostname, puerto, SecureSocketOptions.SslOnConnect);
imap.Authenticate(username, password);

Exception StackTrace:

   en System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   en System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   en System.Net.Sockets.Socket.Connect(IPAddress address, Int32 port)
   en MailKit.Net.Imap.ImapClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)
   en Utilities.Imap4.Imap.Connect(String hostname, String username, String password, ConnectionType secure, Int32 port) en xxxx\Imap4\Imap4\Imap4.cs:línea 121

Upvotes: 1

Views: 781

Answers (1)

jstedfast
jstedfast

Reputation: 38563

The Timeout property does not affect the Connect method and it turns out that the Timeout property on a Socket has the same limitation. There is a StackOverflow answer on how to abort a Socket.Connect() after a timeout, but it is a bit ugly. You could implement that answer and then call ImapClient.Connect() and pass it the Socket.

Upvotes: 1

Related Questions