Search code examples
imapchilkat

"BAD Could not parse command" is returned if mailbox name contains non-English symbols


I'm using Chilkat.IMAP components to get emails from IMAP servers. If a mailbox name contains non-English symbols, "BAD Could not parse command" is returned:

----IMAP REQUEST----
aaai LIST "[Gmail]/" "%"
----IMAP RESPONSE----
* LIST (\All \HasNoChildren) "/" "[Gmail]/All Mail"
* LIST (\HasChildren \Trash) "/" "[Gmail]/Bin"
* LIST (\Drafts \HasNoChildren) "/" "[Gmail]/Drafts"
* LIST (\HasNoChildren \Important) "/" "[Gmail]/Important"
* LIST (\HasNoChildren \Sent) "/" "[Gmail]/Sent Mail"
* LIST (\HasNoChildren \Junk) "/" "[Gmail]/Spam"
* LIST (\HasNoChildren) "/" "[Gmail]/&BB8EMAQ,BDoEMA-"
aaai OK Success

----IMAP REQUEST----
aaaj LIST "[Gmail]/All Mail/" "%"
----IMAP RESPONSE----
aaaj OK Success

----IMAP REQUEST----
aaap LIST "[Gmail]/Папка/" "%"
----IMAP RESPONSE----
aaap BAD Could not parse command

Solution

  • IMAP by default does not send 8-bit characters, and the original protocol defines mailboxes with non-English ASCII characters to be UTF-7 encoded (with some modifications). This is the &BB8EMAQ,BDoEMA- you're seeing.

    You can either add UTF-7 encoding/decoding to your application, or, if your server is new enough, ENABLE UTF-8 mode. Note: enabling UTF-8 may get you Unicode in places you do not expect. Gmail does support this extension.

    > a LIST "" *
    < ...
    < * LIST (\HasChildren) "/" "&AOk-cole"
    
    > b ENABLE UTF8=ACCEPT
    < ...
    < * LIST (\HasChildren) "/" "école"
    

    Here's how that UTF-7 string breaks down:

    [Gmail]/&BB8EMAQ,BDoEMA-
    

    & and - shift in and out of decoding mode, so this looks like

    "[Gmail]/" + mUTF7decode("BB8EMAQ,BDoEMA")
    

    And here's a python 3 one liner that decodes that. With "===" added to meet the base64 padding requirements, and the altchars specifying the last two characters of the base64 encoding:

    >>> import base64;  base64.b64decode("BB8EMAQ,BDoEMA===", altchars="+,").decode("utf-16be")
    'Папка'