Syntax error in BODYSTRUCTURE. Unexpected token: '('
Hi, I have checked internet but can't find a solution for this. I have few email addresses from the same server which are working fine. There is only this one email which is generating this error and not downloading emails. It's using imap, here is the detailed error
at MailKit.Net.Imap.ImapUtils.ReadNStringToken(ImapEngine engine, String format, Boolean rfc2047, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseContentType(ImapEngine engine, String format, CancellationToken cancellationToken, ContentType& contentType, String& value)
at MailKit.Net.Imap.ImapUtils.ParseBody(ImapEngine engine, String format, String path, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseMultipart(ImapEngine engine, String format, String path, String subtype, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseBody(ImapEngine engine, String format, String path, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapFolder.FetchSummaryItems(ImapEngine engine, ImapCommand ic, Int32 index)
at MailKit.Net.Imap.ImapEngine.ProcessUntaggedResponse(CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapCommand.Step()
at MailKit.Net.Imap.ImapEngine.Iterate()
at MailKit.Net.Imap.ImapEngine.Wait(ImapCommand ic)
at MailKit.Net.Imap.ImapFolder.Fetch(IList`1 uids, MessageSummaryItems items, CancellationToken cancellationToken)
at OutReachEmailDownload.MailKitNet.GetAllMails(DateTime FromDateTime, EmailAccount emailAccount, DbContext _dbContext) in MailKitNet.cs:line 125
Any help would be great.
In cases like this, the stack trace is not enough to solve the problem. I really need a protocol log so that I can trace exactly what is causing the problem.
You can read up on how to get a protocol log here: https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog
Here is the log
Connected to imap://mail.thecanvasprints.co.uk:143/?starttls=when-available
S: * OK IMAPrev1
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 CHILDREN IDLE QUOTA SORT ACL NAMESPACE RIGHTS=texk
S: A00000000 OK CAPABILITY completed
C: A00000001 LOGIN
S: A00000001 OK LOGIN completed
C: A00000002 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 CHILDREN IDLE QUOTA SORT ACL NAMESPACE RIGHTS=texk
S: A00000002 OK CAPABILITY completed
C: A00000003 NAMESPACE
S: * NAMESPACE (("" ".")) NIL (("#Public" "."))
S: A00000003 OK namespace command complete
C: A00000004 LIST "" "INBOX"
S: * LIST (\HasNoChildren) "." "INBOX"
S: A00000004 OK LIST completed
C: A00000005 EXAMINE INBOX
S: * 324 EXISTS
S: * 323 RECENT
S: * FLAGS (\Deleted \Seen \Draft \Answered \Flagged)
S: * OK [UIDVALIDITY 1463070126] current uidvalidity
S: * OK [UNSEEN 77] unseen messages
S: * OK [UIDNEXT 411] next uid
S: * OK [PERMANENTFLAGS ()] limited
S: A00000005 OK [READ-ONLY] EXAMINE completed
C: A00000006 UID SEARCH SINCE 11-May-2016 UNDELETED
S: * SEARCH 77 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
S: A00000006 OK UID completed
C: A00000007 UID FETCH 77,88:410 (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODYSTRUCTURE)
S: * 1 FETCH (UID 77 RFC822.SIZE 20376 FLAGS () INTERNALDATE "27-May-2016 17:41:38 +0100" ENVELOPE ("Fri, 27 May 2016 12:00:00 -0400" "But it was just a joke..." (("B21 Employment Law Resources" NIL "training" "rply.b21pubs.com")) (("B21 Employment Law Resources" NIL "training" "rply.b21pubs.com")) (("[email protected]" NIL "training" "b21pubs.com")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<LYRIS-5858349-557511-2016.05.27-12.00.07--juliet.halliwell#[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 2989 108)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 15839 342) "ALTERNATIVE" ("BOUNDARY" "MIMEBoundary3ec4235c942912edbb2cd8cd2a6669f2") NIL NIL))
S: * 2 FETCH (UID 88 RFC822.SIZE 10426 FLAGS () INTERNALDATE "31-May-2016 01:21:11 +0100" ENVELOPE ("Tue, 31 May 2016 07:21:32 +0700" "Signed copy" (("Ernesto Hancock" NIL "HancockErnesto4241" "static.vnpt.vn")) (("Ernesto Hancock" NIL "HancockErnesto4241" "static.vnpt.vn")) (("Ernesto Hancock" NIL "HancockErnesto4241" "static.vnpt.vn")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "windows-1251") NIL NIL "base64" 356 5)( "X-ZIP" ("BOUNDARY" "") NIL NIL) "MIXED" ("BOUNDARY" "--cd49a2f5ed4ed0cbb6f9f1c7f125541f") NIL NIL))
S: * 3 FETCH (UID 89 RFC822.SIZE 211448 FLAGS () INTERNALDATE "31-May-2016 07:25:36 +0100" ENVELOPE ("Tue, 31 May 2016 06:26:04 GMT" "=?iso-8859-1?B?IkFydGlsbGVyaWV0IHN0dWRpbyBpbiByaWNoIGRhcmsgdG9uZXMgYW5kIGxpZ2h0IHBpbmsgYWNjZW50cyIgYnkgbXkgc2NhbmRpbmF2aWFuIGhvbWU=?=" (("Bloglovin" NIL "info" "bloglovin.com")) (("Bloglovin" NIL "info" "bloglovin.com")) (("Bloglovin" NIL "info" "bloglovin.com")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 8122 106)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 201064 2581) "ALTERNATIVE" ("BOUNDARY" "------------jv7xaqkcc36km98dmsdsh4a88") NIL NIL))
S: * 4 FETCH (UID 90 RFC822.SIZE 28690 FLAGS () INTERNALDATE "31-May-2016 11:50:20 +0100" ENVELOPE ("Tue, 31 May 2016 06:00:00 -0400" "Before you sort and filter..." (("Finance webinars - Business 21" NIL "conferences" "rply.b21pubs.com")) (("Finance webinars - Business 21" NIL "conferences" "rply.b21pubs.com")) (("[email protected]" NIL "training" "b21pubs.com")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<LYRIS-5858349-557924-2016.05.31-06.01.07--juliet.halliwell#[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 3765 138)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 23368 462) "ALTERNATIVE" ("BOUNDARY" "MIMEBoundary02dd2571389b384c98215fb1b9881069") NIL NIL))
S: * 5 FETCH (UID 91 RFC822.SIZE 10495 FLAGS () INTERNALDATE "31-May-2016 16:16:53 +0100" ENVELOPE ("Tue, 31 May 2016 16:17:18 +0100" "RE: Guest Posting" (("Katie" NIL "katie" "piggy-payday.co.uk")) (("[email protected]" NIL "finance9" "zoho.com")) (("Katie" NIL "katie" "piggy-payday.co.uk")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "7bit" 537 36)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 8472 109) "ALTERNATIVE" ("BOUNDARY" "----=_Part_6712_1902590291.1464707838177") NIL NIL))
S: * 6 FETCH (UID 92 RFC822.SIZE 122110 FLAGS () INTERNALDATE "31-May-2016 16:17:39 +0100" ENVELOPE ("Tue, 31 May 2016 15:18:08 GMT" "How To Create A Gallery Wall, 4 Modern And Chic Ideas For Your Home Office & other inspiring stories" (("Bloglovin" NIL "info" "bloglovin.com")) (("Bloglovin" NIL "info" "bloglovin.com")) (("Bloglovin" NIL "info" "bloglovin.com")) (("[email protected]" NIL "juliet.halliwell" "thecanvasprints.co.uk")) NIL NIL NIL "<[email protected]>") BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 4752 62)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "quoted-printable" 115091 1478) "ALTERNATIVE" ("BOUNDARY" "------------zkhnyxz7mghwmhhrebu3hwahc") NIL NIL))
S: * 7 FETCH (UID 93 RFC822.SIZE 21013 FLAGS () INTERNALDATE "31-May-2016 18:17:51 +0100" ENVELOPE ("Tue, 31 May 2016 12:00:00 -0400" "YOU might be the problem..." (("Employee Performance Growth Resources" NIL "training" "rply.b21pubs.com")) (("Employee Performance Growth Resources" NIL "training" "rply.b21pubs.com")) (("[email protected]" NIL "training
most likely the problem is this:
.... (("TEXT" "PLAIN" ("CHARSET" "windows-1251") NIL NIL "base64" 356 5)( "X-ZIP" ("BOUNDARY" "") ....
The "X-ZIP" bit is not a full mime-type. Notice how the previous mime-part has a mime-type defined as "TEXT" "PLAIN" (there MUST be 2 strings, a type and subtype).
The server is sending brokenly formatted responses :(
Hmm so any idea how to fix this?
Complain very loudly to your IMAP provider until they fix it :)
Is there anyway to fix in Mailkit?
I can ask them, what do I tell them in IMAP language? Because most of them are not good in understand if I give them .net error.
Thanks for helping.
Problem is if I knew which uid has problem, I can skip them. I could loop one by one and catch them actually but is there anyway to download rest of the message and skip the part where mime-type is broken?
I have tried looping through each message to download but as soon as the problem message comes, it disconnects imap, anyway around this?
The important bit to explain to them is that they need to properly follow the ABNF grammar outlined in rfc3501:
body = "(" (body-type-1part / body-type-mpart) ")"
body-extension = nstring / number /
"(" body-extension *(SP body-extension) ")"
; Future expansion. Client implementations
; MUST accept body-extension fields. Server
; implementations MUST NOT generate
; body-extension fields except as defined by
; future standard or standards-track
; revisions of this specification.
body-ext-1part = body-fld-md5 [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-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-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
body-fld-enc SP body-fld-octets
body-fld-desc = nstring
body-fld-dsp = "(" string SP body-fld-param ")" / nil
body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
"QUOTED-PRINTABLE") DQUOTE) / string
body-fld-id = nstring
body-fld-lang = nstring / "(" string *(SP string) ")"
body-fld-loc = nstring
body-fld-lines = number
body-fld-md5 = nstring
body-fld-octets = number
body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
body-type-1part = (body-type-basic / body-type-msg / body-type-text)
[SP body-ext-1part]
body-type-basic = media-basic SP body-fields
; MESSAGE subtype MUST NOT be "RFC822"
body-type-mpart = 1*body SP media-subtype
[SP body-ext-mpart]
body-type-msg = media-message SP body-fields SP envelope
SP body SP body-fld-lines
body-type-text = media-text SP body-fields SP body-fld-lines
...
media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" /
"MESSAGE" / "VIDEO") DQUOTE) / string) SP
media-subtype
; Defined in [MIME-IMT]
media-message = DQUOTE "MESSAGE" DQUOTE SP DQUOTE "RFC822" DQUOTE
; Defined in [MIME-IMT]
media-subtype = string
; Defined in [MIME-IMT]
media-text = DQUOTE "TEXT" DQUOTE SP media-subtype
; Defined in [MIME-IMT]
In this particular case, they failed to properly encode the body-type-basic's media-basic tokens. It appears to me that they did not send a media-type token, they only sent a media-subtype token. They need to send both. It is illegal to send only one.
I've committed a patch which may work around it, but you still need to harass your IMAP admins to fix their software because the patch in MailKit is a guess that might not always hold true.
As always server people don't know about and they are arguing if it works with your outlook then it's not their problem. I have a dedicated server and HmailServer is installed on it as mail server so I can change the settings, do you know what setting to look for?
I'm not familiar with hMailServer, so I can't help you there :(
I've released MailKit 1.4.1 to nuget.org - if that doesn't fix it, then I'm out of solutions.
I still highly recommend contacting your admins and passing along the bug info
I've been adding unit tests and wrote one up for this. Seems my fix made it get farther in the parsing, but the "X-ZIP" body part is also missing the body-fld-enc and body-fld-octets tokens which my parser doesn't currently handle.
Is this still an issue for you? Or can I (hopefully) ignore the remaining issues?
Hi , I'v got the same exception
MailKit.Net.Imap.ImapProtocolException: Syntax error in BODYSTRUCTURE. Unexpected token: ')'
at MailKit.Net.Imap.ImapUtils.ReadNStringTokenAsync(ImapEngine engine, String format, Boolean rfc2047, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseBodyAsync(ImapEngine engine, String format, String path, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseMultipartAsync(ImapEngine engine, String format, String path, String subtype, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapUtils.ParseBodyAsync(ImapEngine engine, String format, String path, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapFolder.FetchSummaryItemsAsync(ImapEngine engine, ImapCommand ic, Int32 index, Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.ProcessUntaggedResponseAsync(Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Imap.ImapCommand.StepAsync(Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.IterateAsync(Boolean doAsync)
at MailKit.Net.Imap.ImapEngine.RunAsync(ImapCommand ic, Boolean doAsync)
at MailKit.Net.Imap.ImapFolder.FetchAsync(IList`1 uids, MessageSummaryItems items, HashSet`1 fields, Boolean doAsync, CancellationToken cancellationToken)
It failed on this particular fetch :
IMAP | Server : * 56697 FETCH (X-GM-THRID 1563190496481790748 X-GM-MSGID 1563491715126921418 X-GM-LABELS ("\\Sent") UID 227195 RFC822.SIZE 87705 MODSEQ (10677203) INTERNALDATE "01-Apr-2017 15:51:32 +0000" FLAGS (\Seen) ENVELOPE ("Sat, 01 Apr 2017 15:51:32 +0000" "=?UTF-8?B?RndkOiDXodeo15nXp9eqINeq157Xldeg15XXqg==?=" (("..." NIL "..." "gmail.com")) (("...." NIL "..." "gmail.com")) (("... ..." NIL "..." "gmail.com")) (("[email protected]" NIL "..." "....com")) NIL NIL "<[email protected]>" "<[email protected]>") BODYSTRUCTURE ((("TEXT" "PLAIN" ("CHARSET" "UTF-8" "DELSP" "yes" "FORMAT" "flowed") NIL NIL "BASE64" 10418 133 NIL NIL NIL)("TEXT" "HTML" ("CHARSET" "UTF-8") NIL NIL "BASE64" 34544 442 NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "94eb2c1cd0507723d5054c1ce6cb") NIL NIL)("RELATED" NIL ("ATTACHMENT" NIL) NIL)("RELATED" NIL ("ATTACHMENT" NIL) NIL)("RELATED" NIL ("ATTACHMENT" NIL) NIL)("RELATED" NIL ("ATTACHMENT" NIL) NIL) "MIXED" ("BOUNDARY" "94eb2c1cd0507723e6054c1ce6cd") NIL NIL) BODY[HEADER.FIELDS (JOB-ID KEYWORDS LIST-ARCHIVE LIST-HELP LIST-ID LIST-ID LIST-OWNER LIST-POST LIST-SUBSCRIBE LIST-UNSUBSCRIBE LIST-UNSUBSCRIBE-POST LIST-ID LIST-UNSUBSCRIBE MESSAGEID MIME-VERSION MIME-VERSION MAILING-LIST MCIPGROUP)] {21}
MIME-Version: 1.0
)
I guess it's related to the "RELATED" part , right? there is any way I can ignore those parts?
@chenmach can you submit a new bug report for this one? It's similar but different.
Thanks!
I've just slapped your FETCH response into a unit test and I am able to reproduce the parse exception that you are hitting, so I'll probably try to work on fixing this after work tonight.
FWIW, it's also similar to issue #205
Sure :) https://github.com/jstedfast/MailKit/issues/777 thanks!!!
Sorry @jstedfast for asking it here. But may be you can help, why body-fld-octets is defined as 'number' in RFC but all responses from servers return number SP number where second one seems to be lines count or smth like this. Even RFC itself provides two numbers as example.
Sorry: I just realized why this is hapenning. I'm writing parser and it matches rule media-basic instead of media-text. Because media basic defined in a way that it should match "TEXT":
media-basic = ((DQUOTE ("APPLICATION" / "AUDIO" / "IMAGE" / "MESSAGE" / "VIDEO") DQUOTE) / string) SP media-subtype
it matches it as: (... / string) SP media-subtype because string itself allows quoted text. Seems like mistake in the grammar.