Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax error in UIDNEXT response code. Unexpected token: [atom: 0] #1010

Closed
infinity223 opened this issue Apr 11, 2020 · 3 comments
Closed
Labels
compatibility Compatibility with existing software server-bug The bug appears to be in the server

Comments

@infinity223
Copy link

infinity223 commented Apr 11, 2020

Hi there!

C#, Windows, 64bit, .NET 4.5.2, latest nuget version.

I got a pretty straight forward code for logging into the mail account, checking all folders (with count of messages) and printing them out in a richtextbox.

                                using (var client = new ImapClient(new ProtocolLogger("imap.log")))
                                {
                                    client.Connect(imapHost, imapPort, useSSL);
                                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                                    client.Authenticate(login, haslo);

                                    foreach (var folder in client.GetFolders(client.PersonalNamespaces[0]))
                                    {
                                        folder.Open(FolderAccess.ReadOnly);
                                        //richTextBox2.AppendText("\n" + folder.Name + ": ");
                                        //richTextBox2.AppendText(folder.Count.ToString());
                                    }
                                    client.Disconnect(true);
                                }

client.Connect is provided with correct informations, checked it three times, same as Authenticate.

Problem is with

folder.Open(FolderAccess.ReadOnly);

.. and only on some of the hosts, not all of them (right now I found that problem on two).

Here is what exception is throwing out:

MailKit.Net.Imap.ImapProtocolException: Syntax error in UIDNEXT response code. Unexpected token: [atom: 0]
   w MailKit.Net.Imap.ImapEngine.ParseNumber(ImapToken token, Boolean nonZero, String format, Object[] args)
   w MailKit.Net.Imap.ImapEngine.<ParseResponseCodeAsync>d__167.MoveNext()

Logs says:

Connected to imaps://poczta.o2.pl:993/
S: * OK IMAP4 ready
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4rev1 UIDPLUS LITERAL+ CHILDREN NAMESPACE XLIST ID IDLE MOVE XAOL-MOVE AUTH=PLAIN
S: A00000000 OK completed
C: A00000001 AUTHENTICATE PLAIN
S: + 
C: AGJ5YnZ5bmVhaHYuZWJsdGhqdGZmbUBvMi5wbABHWUtUTS5rdHVkMzM5
S: A00000001 OK Login completed
C: A00000002 CAPABILITY
S: * CAPABILITY IMAP4rev1 UIDPLUS LITERAL+ CHILDREN NAMESPACE XLIST ID IDLE MOVE XAOL-MOVE
S: A00000002 OK CAPABILITY completed
C: A00000003 NAMESPACE
S: * NAMESPACE (("" "/")) NIL NIL
S: A00000003 OK NAMESPACE completed
C: A00000004 LIST "" "INBOX"
S: * LIST (\Noinferiors) "/" "INBOX"
S: A00000004 OK List completed
C: A00000005 XLIST "" "*"
S: * XLIST (\Noinferiors \Drafts) "/" "Drafts"
S: * XLIST (\Noinferiors) "/" "INBOX"
S: * XLIST (\Noinferiors \Archive) "/" "INBOX/Archive"
S: * XLIST (\HasChildren) "/" "Segregator"
S: * XLIST (\HasNoChildren) "/" "Segregator/Newslettery"
S: * XLIST (\HasNoChildren) "/" "Segregator/Oferty"
S: * XLIST (\HasNoChildren) "/" "Segregator/Spo&AUI-eczno&AVs-ci"
S: * XLIST (\Noinferiors \Sent) "/" "Sent"
S: * XLIST (\Noinferiors \Junk) "/" "Spam"
S: * XLIST (\Noinferiors \Trash) "/" "Trash"
S: A00000005 OK List completed
C: A00000006 LIST "" "*"
S: * LIST (\Noinferiors \Drafts) "/" "Drafts"
S: * LIST (\Noinferiors) "/" "INBOX"
S: * LIST (\Noinferiors \Archive) "/" "INBOX/Archive"
S: * LIST (\HasChildren) "/" "Segregator"
S: * LIST (\HasNoChildren) "/" "Segregator/Newslettery"
S: * LIST (\HasNoChildren) "/" "Segregator/Oferty"
S: * LIST (\HasNoChildren) "/" "Segregator/Spo&AUI-eczno&AVs-ci"
S: * LIST (\Noinferiors \Sent) "/" "Sent"
S: * LIST (\Noinferiors \Junk) "/" "Spam"
S: * LIST (\Noinferiors \Trash) "/" "Trash"
S: A00000006 OK List completed
C: A00000007 EXAMINE Drafts
S: * 0 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 0] UIDs valid
S: * OK [UIDVALIDITY 0] UIDs valid
S: * OK [UIDNEXT 0] Predicted next UID
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS ()]
S: * OK [NOMODSEQ] Sorry, this mailbox format doesn't support modsequences
S: A00000007 OK [READ-ONLY] SELECT completed

draftbox is clear (0 messages).
I checked it on other host, with clear mailbox and UIDNEXT is 1.

I did check skipping "Drafts" folder, and on clear INBOX I got:

C: B00000007 EXAMINE INBOX
S: * 0 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 0] UIDs valid
S: * OK [UIDVALIDITY 0] UIDs valid
S: * OK [UIDNEXT 0] Predicted next UID
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS ()]
S: * OK [NOMODSEQ] Sorry, this mailbox format doesn't support modsequences

So it looks like when I got clear mailbox folder for some cases UIDNEXT is 0.
On a full mailbox folder (like 700 messages) it seems that it is working just fine.

@jstedfast
Copy link
Owner

jstedfast commented Apr 11, 2020

Hello!

Thanks so much for providing the protocol logs! 😍

As you have discovered, the problem is that the UIDNEXT value is 0.

The syntax grammar, according to rfc3501 (page 85):

resp-text-code  = "ALERT" /
                  "BADCHARSET" [SP "(" astring *(SP astring) ")" ] /
                  capability-data / "PARSE" /
                  "PERMANENTFLAGS" SP "("
                  [flag-perm *(SP flag-perm)] ")" /
                  "READ-ONLY" / "READ-WRITE" / "TRYCREATE" /
                  "UIDNEXT" SP nz-number / "UIDVALIDITY" SP nz-number /
                  "UNSEEN" SP nz-number /
                  atom [SP 1*<any TEXT-CHAR except "]">]

As we see here, the value for both UIDNEXT and UIDVALIDITY are supposed to be nz-number, which is defined as:

nz-number       = digit-nz *DIGIT
                    ; Non-zero unsigned 32-bit integer
                    ; (0 < n < 4,294,967,296)

This is why MailKit is throwing the exception.

That said, you'll probably notice based on the syntax above, that MailKit isn't throwing an exception for UIDVALIDITY which is also 0 in your logs and is parsed before UIDNEXT (since it appears before UIDNEXT in the logs). Same with UNSEEN.

The reason for this is because MailKit already has work-arounds for those.

The reason that UIDNEXT is not allowed (by the spec) to be 0 is because a UID of 0 is not allowed:

uniqueid        = nz-number
                    ; Strictly ascending

Anyway, I'll add a work-around for UIDNEXT as well so it won't throw an exception. Syntax errors like this are generally trivial to deal with ;-)

jstedfast added a commit that referenced this issue Apr 11, 2020
@jstedfast jstedfast added compatibility Compatibility with existing software server-bug The bug appears to be in the server labels Apr 11, 2020
@infinity223
Copy link
Author

Thanks so much for the fix. Is there a way to push it to nuget?

Tried build master branch but I got errors sadly :(

@jstedfast
Copy link
Owner

FWIW, every commit results in a new automated build/nuget package published to https://www.myget.org/feed/mimekit/package/nuget/MailKit

These packages are not "official releases", but they are very handy in situations like this ;-)

I normally only do a release/push to nuget.org every few months when I've collected enough fixes that I feel it's worth doing. Otherwise there'd be days where there are like 10+ new versions released ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility Compatibility with existing software server-bug The bug appears to be in the server
Projects
None yet
Development

No branches or pull requests

2 participants