{"record":{"id":"a8ff9ec76b87501b","repo":"jstedfast/MailKit","slug":"the-imap-server-did-not-return-the-requested-message-headers","errorCode":null,"errorMessage":"The IMAP server did not return the requested message headers.","messagePattern":"The IMAP server did not return the requested message headers\\.","errorType":"exception","errorClass":"MessageNotFoundException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":2541,"sourceCode":"\t\t\t\tthrow new ArgumentException (\"The uid is invalid.\", nameof (uid));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tvar ic = new ImapCommand (Engine, cancellationToken, this, \"UID FETCH %u (BODY.PEEK[HEADER])\\r\\n\", uid.Id);\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)\n\t\t{\n\t\t\tProcessFetchResponse (ic);\n\n\t\t\tif (!ctx.TryGetSection (uid, \"HEADER\", out var section, true))\n\t\t\t\tthrow new MessageNotFoundException (\"The IMAP server did not return the requested message headers.\");\n\n\t\t\treturn section.Stream;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the specified message headers.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the specified message headers.\n\t\t/// </remarks>\n\t\t/// <returns>The message headers.</returns>\n\t\t/// <param name=\"uid\">The UID of the message.</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.ArgumentException\">\n\t\t/// <paramref name=\"uid\"/> is invalid.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ObjectDisposedException\">","sourceCodeStart":2523,"sourceCodeEnd":2559,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L2523-L2559","documentation":"MessageNotFoundException thrown by ProcessGetHeadersResponse when the IMAP server's FETCH response did not include a BODY[HEADER] section for the requested UID. MailKit queued a UID FETCH (BODY.PEEK[HEADER]) command, but no matching section arrived — typically because the message no longer exists on the server. It signals the requested data is unobtainable rather than a bad argument.","triggerScenarios":"Calling ImapFolder.GetHeaders(UniqueId uid, ...) (or async) for a message that was expunged concurrently, on a server that silently omits the section, or after the message set changed between the search and the fetch.","commonSituations":"Another client or auto-expunge deleted the message between your Search and GetHeaders calls; server-side junk cleanup removed messages mid-session; UIDVALIDITY changed and the UID maps to a nonexistent message.","solutions":["Catch MessageNotFoundException and treat the message as deleted; skip it and continue.","Re-fetch the folder summary/Search to get a current message list, then retry with a valid UID.","Check the folder's UIDValidity; if it changed, invalidate all cached UIDs and re-enumerate messages."],"exampleFix":"// before\nvar headers = folder.GetHeaders(uid);\n\n// after\nHeaderList headers;\ntry {\n    headers = folder.GetHeaders(uid);\n} catch (MessageNotFoundException) {\n    continue; // message was deleted server-side\n}","handlingStrategy":"try-catch","validationCode":"var summary = folder.Fetch(new[] { uid.Id }, MessageSummaryItems.UniqueId, cancellationToken: ct);\nif (summary.Length == 0) throw new MessageNotFoundException(\"Message no longer exists on the server.\");","typeGuard":null,"tryCatchPattern":"try { var headers = folder.GetHeaders(uid); }\ncatch (MessageNotFoundException) { /* treat as deleted: remove from local cache and skip */ }","preventionTips":["Catch MessageNotFoundException in any pipeline processing messages by UID over long periods.","Re-sync the folder when UIDValidity changes; discard all cached UIDs.","Prefer fetching summaries in batches, then fetch headers promptly to shrink the deletion window."],"tags":["imap","message-not-found","mailkit","server-response"],"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"}