{"record":{"id":"c38ff0979f47a849","repo":"jstedfast/MailKit","slug":"the-imap-server-did-not-return-the-requested-message","errorCode":null,"errorMessage":"The IMAP server did not return the requested message.","messagePattern":"The IMAP server did not return the requested message\\.","errorType":"exception","errorClass":"MessageNotFoundException","httpStatus":null,"severity":"warning","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":3329,"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[])\\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 ProcessGetMessageResponse (ImapCommand ic, FetchStreamContext ctx, UniqueId uid)\n\t\t{\n\t\t\tProcessFetchResponse (ic);\n\n\t\t\tif (!ctx.TryGetSection (uid, string.Empty, out var section, true))\n\t\t\t\tthrow new MessageNotFoundException (\"The IMAP server did not return the requested message.\");\n\n\t\t\treturn section.Stream;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get the specified message.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the specified message.\n\t\t/// </remarks>\n\t\t/// <returns>The message.</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":3311,"sourceCodeEnd":3347,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L3311-L3347","documentation":"ProcessGetMessageResponse throws MessageNotFoundException when, after processing the FETCH response, the server did not return the requested message section. This happens when the message was expunged between listing and fetching, or the server silently omitted it. It means the UID no longer resolves to retrievable content on the server.","triggerScenarios":"Message expunged by another client between folder.Fetch and GetMessage; UIDVALIDITY changed so the UID no longer maps to a message; server-side race where the FETCH returns no matching untagged FETCH response.","commonSituations":"Long-lived mailbox connections with aggressive server-side cleanup (spam purge); multiple clients on the same account; fetching messages by cached UIDs from a previous session.","solutions":["Catch MessageNotFoundException and treat the message as gone; refresh the summary.","Verify folder.UidValidity has not changed; re-open the folder if it did.","Retry the fetch once after a NOOP/examine to re-sync flags and expunges."],"exampleFix":"// before\nvar msg = folder.GetMessage(uid);\n// after\ntry {\n    var msg = folder.GetMessage(uid);\n} catch (MessageNotFoundException) {\n    // message was expunged server-side; refresh and continue\n    folder.NoOp();\n}","handlingStrategy":"try-catch","validationCode":"if (folder.UidValidity != savedUidValidity) { /* reopen folder and refetch summaries */ }","typeGuard":null,"tryCatchPattern":"try { return folder.GetMessage(uid); }\ncatch (MessageNotFoundException) {\n    // expunged server-side: resync and treat as deleted\n    folder.NoOp();\n    return null;\n}","preventionTips":["Catch MessageNotFoundException around all UID fetches","Track folder.UidValidity and reopen on change","Minimize delay between listing and fetching","Keep summary state updated with count-changed/expunge events"],"tags":["imap","message-not-found","race-condition","mailkit"],"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"}