jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.78k stars 360 forks source link

Connecting to mail server sometimes takes forever #147

Closed Sicos1977 closed 9 years ago

Sicos1977 commented 9 years ago

Hi,

I have a question about the code below. Does the client.Connect method use Tasks? And if so is it then possible that this sometimes can take forever to complete because I use a lot of tasks on the same system? The reason why I ask this is that the client.Connect method sometimes takes forever to complete. When I run the code from Visual Studio then it runs as expected. It only gives problems when it is deployed on a Windows 2012 R2 IIS 8.5 system.

            using (var client = new ImapClient())
            {
                client.Connect(Server, 993, true);
                client.AuthenticationMechanisms.Remove("XOAUTH");
                client.Authenticate(User, Password);
                client.Inbox.Open(FolderAccess.ReadOnly);

                const MessageSummaryItems items = 
                    MessageSummaryItems.UniqueId |
                    MessageSummaryItems.Flags |
                    MessageSummaryItems.Envelope | 
                    MessageSummaryItems.MessageSize;

                var fields = new HashSet<HeaderId> {HeaderId.Importance};
                var result = client.Inbox.Count == 0 ? new IMessageSummary[0] : client.Inbox.Fetch(0, -1, items, fields);
                client.Disconnect(true);
                return result;
            }
jstedfast commented 9 years ago

Nope, it doesn't use tasks. It's probably just network slowness. All it does is call Socket.Connect().

Sicos1977 commented 9 years ago

Isn't this code using tasks?

            try {
                cancellationToken.ThrowIfCancellationRequested ();
                socket.Connect (ipAddresses[i], port);
                break;
            } catch (OperationCanceledException) {
                socket.Dispose ();
                throw;
            } catch {
                socket.Dispose ();

                if (i + 1 == ipAddresses.Length)
                    throw;
            }
jstedfast commented 9 years ago

No. Where do you see tasks? It's using a CancellationToken, but that is not a task.

Sicos1977 commented 9 years ago

The cancellation token was my mistake... but still got a problem... this is the connect logging that I have logged with the protocollogger. Any ideas why I can't get a list of messages?

The log is written from the exception handler that I did put around the code

Connected to imaps://server:993/ S: * OK The IMAP4 service is ready. C: A00000000 CAPABILITY S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000000 OK CAPABILITY completed.

jstedfast commented 9 years ago

Looks like the authentication process hasn't completed. MailKit looks to be waiting for the server to respond.

Sicos1977 commented 9 years ago

And this is the logging when I run it from visual studio.

Connected to imaps://server:993/ C: A00000000 CAPABILITY S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000000 OK CAPABILITY completed. C: A00000001 AUTHENTICATE PLAIN S: + C: .... <base 64 encoded password string> S: A00000001 OK AUTHENTICATE completed. C: A00000002 CAPABILITY S: * CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN UIDPLUS MOVE ID CHILDREN IDLE NAMESPACE LITERAL+ S: A00000002 OK CAPABILITY completed. C: A00000003 NAMESPACE S: * NAMESPACE (("" "/")) NIL NIL S: A00000003 OK NAMESPACE completed. C: A00000004 LIST "" "INBOX" S: * LIST (\Marked \HasNoChildren) "/" INBOX S: A00000004 OK LIST completed. C: A00000005 EXAMINE INBOX S: * 0 EXISTS S: * 0 RECENT S: * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent) S: * OK [PERMANENTFLAGS ()] Permanent flags S: * OK [UIDVALIDITY 14] UIDVALIDITY value S: * OK [UIDNEXT 20] The next unique identifier value S: A00000005 OK [READ-ONLY] EXAMINE completed. C: A00000006 LOGOUT

jstedfast commented 9 years ago

Yep, this is the line that MailKit is waiting for in your previous comment:

S: +

According to the first log, the server is not sending it.

Sicos1977 commented 9 years ago

Well... just don't understand why this works on my development server and fails on my deployment server. I even did set off the firewall but that doesn't make any difference at all

jstedfast commented 9 years ago

Server load? Network congestion? Some sort of proxy?

Sicos1977 commented 9 years ago

Uhm... forget everything... It seems that there is a problem with the domain controllers inside the company where I'm working... and since this is a very big company (16.000+ people) they forgot to tell us that there is a problem... sigh...

And off course my development platform is on another domain controller then my deployment server Thanks for the help anyway