{"record":{"id":"5d1e21dfd0df0b23","repo":"jstedfast/MailKit","slug":"the-imap-server-did-not-return-the-requested-stream","errorCode":null,"errorMessage":"The IMAP server did not return the requested stream.","messagePattern":"The IMAP server did not return the requested stream\\.","errorType":"exception","errorClass":"MessageNotFoundException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":4214,"sourceCode":"\t\t\t\tic = null;\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tic = new ImapCommand (Engine, cancellationToken, this, \"UID FETCH %u (BODY.PEEK[]<%d.%d>)\\r\\n\", uid.Id, offset, count);\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 true;\n\t\t}\n\n\t\tStream ProcessGetStreamResponse (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 stream.\");\n\n\t\t\treturn section.Stream;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Get a substream of the specified message.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Fetches a substream of the message. If the starting offset is beyond\n\t\t/// the end of the message, an empty stream is returned. If the number of\n\t\t/// bytes desired extends beyond the end of the message, the stream will\n\t\t/// end where the message ends.\n\t\t/// </remarks>\n\t\t/// <returns>The stream.</returns>\n\t\t/// <param name=\"uid\">The UID of the message.</param>\n\t\t/// <param name=\"offset\">The starting offset of the first desired byte.</param>\n\t\t/// <param name=\"count\">The number of bytes desired.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>","sourceCodeStart":4196,"sourceCodeEnd":4232,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L4196-L4232","documentation":"ProcessGetStreamResponse (UID variant) throws MessageNotFoundException when the server's FETCH response does not contain the requested section for the UID. The library asked for BODY[]<offset.count> but the server returned nothing usable, so the requested stream cannot be provided.","triggerScenarios":"Calling GetStream(uid, ...) where ctx.TryGetSection(uid, ...) fails after ProcessFetchResponse (ImapFolderFetch.cs:4214) - the server omitted the section, returned BADCHARSET/nil data, or the message was expunged between request and response.","commonSituations":"Servers (or proxies) that silently skip truncated/invalid BODY[] sections; message deleted concurrently by another client; nonconformant IMAP servers returning partial FETCH responses.","solutions":["Catch MessageNotFoundException and treat as the message no longer existing; refresh summaries.","Verify the message still exists (folder.GetUid / Fetch) before streaming.","Retry once after refreshing the folder's summary state.","Test against a known-conformant server (e.g. Dovecot) to isolate server quirks."],"exampleFix":"// before\nvar stream = folder.GetStream(uid, offset, count);\n// after\ntry {\n    stream = folder.GetStream(uid, offset, count);\n} catch (MessageNotFoundException) {\n    folder.Fetch((int)0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size, null);\n}","handlingStrategy":"try-catch","validationCode":"var summary = folder.GetSummary(uid); // or verify message still listed in summaries\nif (summary is null) return null;","typeGuard":null,"tryCatchPattern":"try { stream = folder.GetStream(uid, offset, count); }\ncatch (MessageNotFoundException) {\n    folder.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size, null);\n    return null; // message vanished server-side\n}","preventionTips":["Handle FolderExpunged/MessagesVanished events to keep state fresh.","Prefer UID overloads so stale items are detectable.","Treat missing streams as transient and re-sync before retrying once.","Test against a conformant server (Dovecot) to rule out server quirks."],"tags":["message-not-found","imap","server-response","mailkit"],"backgroundTag":"resource-not-found","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}