{"record":{"id":"c6e0f135c6261cc3","repo":"jstedfast/MailKit","slug":"the-imap-server-did-not-return-the-requested-body-part","errorCode":null,"errorMessage":"The IMAP server did not return the requested body part headers.","messagePattern":"The IMAP server did not return the requested body part headers\\.","errorType":"exception","errorClass":"MessageNotFoundException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":2681,"sourceCode":"\n\t\t\tCheckState (true, false);\n\n\t\t\tvar command = string.Format (\"UID FETCH {0} ({1})\\r\\n\", uid, GetBodyPartQuery (partSpecifier, true, out tags));\n\t\t\tvar ic = new ImapCommand (Engine, cancellationToken, this, command);\n\t\t\tic.RegisterUntaggedHandler (\"FETCH\", FetchStreamHandler);\n\t\t\tic.UserData = ctx = new FetchStreamContext (progress);\n\n\t\t\tEngine.QueueCommand (ic);\n\n\t\t\treturn ic;\n\t\t}\n\n\t\tStream ProcessGetHeadersResponse (ImapCommand ic, FetchStreamContext ctx, UniqueId uid, string[] tags)\n\t\t{\n\t\t\tProcessFetchResponse (ic);\n\n\t\t\tif (!ctx.TryGetSection (uid, tags[0], out var section, true))\n\t\t\t\tthrow new MessageNotFoundException (\"The IMAP server did not return the requested body part headers.\");\n\n\t\t\treturn section.Stream;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the specified body part headers.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the specified body part headers.\n\t\t/// </remarks>\n\t\t/// <returns>The body part headers.</returns>\n\t\t/// <param name=\"uid\">The UID of the message.</param>\n\t\t/// <param name=\"partSpecifier\">The body part specifier.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <param name=\"progress\">The progress reporting mechanism.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"partSpecifier\"/> is <see langword=\"null\" />.\n\t\t/// </exception>","sourceCodeStart":2663,"sourceCodeEnd":2699,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L2663-L2699","documentation":"MessageNotFoundException thrown by ProcessGetHeadersResponse (body-part overload) when the server's FETCH response lacks the requested BODY[part.HEADER] section. MailKit requested the specified body part's headers, but the server did not return them — usually because the message (and thus the part) no longer exists, or the part specifier no longer matches the message's structure.","triggerScenarios":"Calling ImapFolder.GetHeaders(UniqueId uid, string partSpecifier, ...) with a partSpecifier derived from an old BodyPart tree after the message changed or was expunged, or against a server that omits the section.","commonSituations":"Reusing cached BodyPart specifiers across sessions while server-side filters rewrote messages; message deleted by another client between GetBodyPart and GetHeaders; buggy/limited IMAP servers (e.g., some proxies) not returning the part.","solutions":["Catch MessageNotFoundException and handle the missing message/part gracefully (skip, log, re-sync).","Re-run GetBodyPart/GetBodyParts to obtain a fresh part specifier matching the current message structure.","Verify the message still exists by fetching its summary/flags before requesting part headers."],"exampleFix":"// before\nvar headers = folder.GetHeaders(uid, cachedSpecifier);\n\n// after\ntry {\n    var headers = folder.GetHeaders(uid, cachedSpecifier);\n} catch (MessageNotFoundException) {\n    var body = folder.GetBodyPart(uid, \"1\"); // re-discover current structure\n}","handlingStrategy":"try-catch","validationCode":"var structure = folder.GetBodyParts(uid);\nif (!structure.BodyParts.Any(p => p.PartSpecifier == specifier))\n    throw new MessageNotFoundException($\"Part {specifier} does not exist in current message structure.\");","typeGuard":null,"tryCatchPattern":"try { var headers = folder.GetHeaders(uid, partSpecifier); }\ncatch (MessageNotFoundException) { /* re-fetch BodyPart tree and retry once */ }","preventionTips":["Re-derive part specifiers from a fresh BodyStructure fetch rather than caching across sessions.","Verify the message still exists (summary fetch) before requesting part headers.","Handle MessageNotFoundException as an expected condition in shared-mailbox scenarios."],"tags":["imap","message-not-found","mailkit","bodypart"],"backgroundTag":"record-not-found","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}