1

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
Aleksey
  • 179
  • 1
  • 1
  • 13

2 Answers2

2

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")
'Папка'
Max
  • 10,701
  • 2
  • 24
  • 48
  • Thank you. I will try this approach. I just wonder why Chilkat component converts "[Gmail]/&BB8EMAQ,BDoEMA-" to "[Gmail]/Папка/" in the response and doesn't convert this in a request. – Aleksey Sep 25 '18 at 21:27
  • According to RFC-5738, will "aaap LIST *"[Gmail]/Папка/" "%" work right? Please note there is * followed by mailbox name in double quotes. – Aleksey Sep 25 '18 at 21:38
  • If you've used `ENABLE UTF8=ACCEPT`, yes. Your library should have a function for doing the necessary conversion, or giving you the raw mailbox name rather than the decoded display name. – Max Sep 25 '18 at 21:40
  • It seems, ENABLE UTF8=ACCEPT is not required if a client uses utf8-quoted strings in requests only. In other words, if an IMAP server has a capability "UTF8=ACCEPT", a client can send utf8-quoted strings. The server should send UTF-7 until ENABLE UTF8=ACCEPT is sent. – Aleksey Sep 25 '18 at 21:52
  • That appears not to be true for gmail. They don't actually support the *"utf8string" syntax, but they do support UTF-8 in regular "strings", but for folders, only after ENABLE. Eg, they've only implemented about 10% of the RFC. – Max Sep 25 '18 at 22:01
  • 1
    I've used [this code](https://stackoverflow.com/a/51379904/5433922) to encode mailbox name to modified UTF-7 - works fine. Please note a little fix is required at the code. I left a comment there. – Aleksey Sep 25 '18 at 23:51
0

It may be that you're using a very old version of Chilkat. Try the latest version, it should work fine. If not, please let us know..

Chilkat Software
  • 1,405
  • 1
  • 9
  • 8
  • The latest version (9.5.0.75) has the same issue: ListMailboxes(530ms): DllDate: Aug 25 2018 ChilkatVersion: 9.5.0.75 UnlockPrefix: Start my 30-day Trial Architecture: Little Endian; 32-bit Language: .NET 4.0 ... ImapCmdSent: aaap LIST "[Gmail]/Папка/" "%" getCompleteResponse(530ms): ImapCmdResp: aaap BAD Could not parse command --getCompleteResponse isOK: serverResponse: aaap BAD Could not parse command --isOK Failed. --listMailboxes Failed. --ListMailboxes – Aleksey Sep 25 '18 at 23:44
  • Did you ever find a solution? If not, I know for a fact that my [MailKit](https://github.com/jstedfast/MailKit) .NET library works correctly in this situation. – jstedfast Nov 30 '18 at 12:54