Mailkit Fetch “Unexpected token in IMAP response:

2019-09-18 03:13发布

I'm getting an ImapProtocolException with a message:

"Unexpected token in IMAP response: [qstring: "Multipart message"]" when issuing  zInbox.Fetch(uids, MailKit.MessageSummaryItems.UniqueId Or MailKit.MessageSummaryItems.BodyStructure Or MailKit.MessageSummaryItems.Envelope)

But on the other hand if I use GetMessage(uids) all goes fine, no error.

Here is ProtocolLogger from Fetch (you can notice no Fetch Completed message):

C: A00000790 UID FETCH 97824 (UID ENVELOPE BODYSTRUCTURE)
S: * 24320 FETCH (UID 97824 ENVELOPE ("Mon, 26 Oct 2015 16:48:44    +0000" "NFe" ((NIL NIL "fatsc" "makrocentral.com.br")) NIL NIL ((NIL NIL "fiscal" "generale.ind.br")) NIL NIL NIL "<20151026164845.43268801F3AD@mail-smtp08-mia.tpn.terra.com>") BODYSTRUCTURE (("text" "plain" ("charset" "ISO-8859-1") NIL "Message text" "Quoted-printable" 227 8 NIL ("inline" NIL) NIL NIL)("application" "octet-stream" ("name" "42151000460986000506550040000387401000387404-nfe.xml") NIL "Attached file: 42151000460986000506550040000387401000387404-nfe.xml" "Base64" 11086 NIL ("attachment" ("filename" "42151000460986000506550040000387401000387404-nfe.xml")) NIL NIL)("application" "pdf" ("name" "42151000460986000506550040000387401000387404-nfe.pdf") NIL "Attached file: 42151000460986000506550040000387401000387404-nfe.pdf" "Base64" 50364 NIL ("attachment" ("filename" "42151000460986000506550040000387401000387404-nfe.pdf")) NIL NIL) "mixed" ("boundary" "017AC96C_005ED075_Synapse_boundary") "Multipart message" NIL))

When using GetMessage(uids), Protocologger give me an interesting info:

S: X-mailer: Synapse - Pascal TCP/IP library by Lukas Gebauer

I already have problems with this mailer when using another library.

As I understand "Fetch" belongs to MailKit and "GetMessage" belongs to MimeKit.

So, is there any work-around to proceed with Fetch instead of use GetMessage? Loop through ~1500 mails and download each one just to get attachments names take a very long time?

Does anybody here had a similar problems with mailer like this one "Synapse - Pascal TCP/IP library by Lukas Gebauer"?


So I think now have some more info and questions. Since started this thread I'm dealing with BODYSTRUCTURE and not so much clear (at least to me) RFC 3501. I have back to talk about "Synapse" again, here is string from Log, the that give me Unexpected Token.

S: * 22300 FETCH (UID 32388 ENVELOPE ("Thu, 6 Sep 2018 13:19:10 -0300" "Zebra - XML sent" (("Zebra" NIL "invoice" "zebra.com")) NIL NIL ((NIL NIL "payments" "cat.com")) ((NIL NIL "invoice" "zebra.com") (NIL NIL "mary" "zebra.com")(NIL NIL "igor" "zebra.com")) NIL NIL "<20180906161753.3F4A774030A@proxy.tiger.com>") BODYSTRUCTURE (("text" "plain" ("charset" "UTF-8") NIL "Message text" "Quoted-printable" 209 6 NIL ("inline" NIL) NIL NIL)("text" "xml" ("name" "4441004299066.xml") NIL "4441004299066.xml" "Base64" 10642 137 NIL ("inline" ("filename" "4441004299066.xml")) NIL NIL)("application" "pdf" ("name" "4441004299066.pdf") NIL "4441004299066.pdf" "Base64" 48448 NIL ("inline" ("filename" "4441004299066.pdf")) NIL NIL) "mixed" ("boundary" "6624CFB2_17170C36_Synapse_boundary") "Multipart message" NIL))

As pointed in comments, the problem is at the end part "Multipart message" NIL, on the other hand a BodyStructure that works fine can end with "NIL NIL" or **"NIL pt-br".

So, ok, now the question is. What should be the last 2 fields in BodyStructure string? In mean time I found this: 1) "Multipart message" is coming from "Content-Description" of message header 2) "pt-br" is coming from "Content-Language" of message header

I totally agree to follow RFC, but looks like the last 2 values are not clear from where they should came from. I mean Content-Language or Language-Tag? Content-Disposition or Content-Description?

Since Exchanges Server and outlook,office365.com become widely used, to me sounds I have to stay using GetMessage and forget about Fetch.

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-18 03:58

The problem is that the IMAP server responded with a completely broken response.

According to the IMAP specification, the multipart syntax following the "mixed" token should be:

body-ext-mpart  = body-fld-param [SP body-fld-dsp [SP body-fld-lang
              [SP body-fld-loc *(SP body-extension)]]]
                ; MUST NOT be returned on non-extensible
                ; "BODY" fetch
body-fld-param  = "(" string SP string *(SP string SP string) ")" / nil
body-fld-dsp    = "(" string SP body-fld-param ")" / nil
body-fld-lang   = nstring / "(" string *(SP string) ")"
body-fld-loc    = nstring

but what we got was: ("boundary" "017AC96C_005ED075_Synapse_boundary") "Multipart message" NIL)

This essentially breaks down into the following types of tokens:

body-fld-param [SP] string [SP] nil

Notice how "Multipart message" is not allowed. What should be there is either a NIL or something like: ("inline" ("name" "value"))

In other words, your IMAP server is not speaking grammatically correct IMAP. There's no way that MailKit can understand what your IMAP server is trying to say.

Note: the problem is the IMAP server, not the Synapse Mailer in this case.

查看更多
登录 后发表回答