Search code examples
c#imapmailkit

MailKit.Net.Imap.ImapProtocolException: "Syntax error in IMAP server greeting. Unexpected token: [atom: OK]"


code:

using (var client = new ImapClient(new ProtocolLogger("logImap.txt")))
{
    // For demo-purposes, accept all SSL certificates
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("imap.yandex.ru", 993, true);
    client.Authenticate(login, password);
    // The Inbox folder is always available on all IMAP servers...
    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadOnly);
    Console.WriteLine("Total messages: {0}", inbox.Count);
    Console.WriteLine("Recent messages: {0}", inbox.Recent);
    for (int i = 0; i < inbox.Count; i++)
    {
        var message = inbox.GetMessage(i);
        Console.WriteLine("Subject: {0}", message.Subject);
    }
    client.Disconnect(true);
}

Stack trace:

MailKit.Net.Imap.ImapEngine.AssertToken(MailKit.Net.Imap.ImapToken, MailKit.Net.Imap.ImapTokenType, string, object[]) MailKit.Net.Imap.ImapEngine.ConnectAsync(MailKit.Net.Imap.ImapStream, bool, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult() MailKit.Net.Imap.ImapClient.ConnectAsync(string, int, MailKit.Security.SecureSocketOptions, bool, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) ...

And logImap.txt:

Connected to imaps://imap.yandex.ru:993/
S: OK IMAP4

What can I do with that problem?


Solution

  • This is what I get when I run your program (after commenting out everything after the Connect() call since I don't have a username and password string to use).

    Connected to imaps://imap.yandex.ru:993/
    S: * OK Yandex IMAP4rev1 at iva1-ea72743daf57.qloud-c.yandex.net:993 ready to talk with ::ffff:72.94.35.232:51061, 2020-Mar-20 16:12:10, ACZCR35nKa61
    C: A00000000 CAPABILITY
    S: * CAPABILITY IMAP4rev1 CHILDREN UNSELECT LITERAL+ NAMESPACE XLIST BINARY UIDPLUS ENABLE ID AUTH=PLAIN AUTH=XOAUTH2 IDLE MOVE
    S: A00000000 OK CAPABILITY Completed.
    C: A00000001 LOGOUT
    S: * BYE IMAP4rev1 Server logging out
    S: A00000001 OK LOGOUT Completed.
    

    In your log, the greeting looks different:

    S: OK IMAP4
    

    Notice that the first character in the greeting in my case is a *.

    That suggests that the server you connected to is a broken IMAP server and is not MailKit's fault.

    Yandex appears to be a round-robin of IMAP servers. The one I connected to obviously works, but the one you connected to is broken.