{"record":{"id":"614be6b58938cb1b","repo":"jstedfast/MailKit","slug":"index-imapfolderfetch","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":3053,"sourceCode":"\t\tpublic override async Task<HeaderList> GetHeadersAsync (int index, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\n\t\t{\n\t\t\tvar ic = QueueGetHeadersCommand (index, cancellationToken, progress, out var ctx);\n\n\t\t\ttry {\n\t\t\t\tawait Engine.RunAsync (ic).ConfigureAwait (false);\n\n\t\t\t\tvar stream = ProcessGetHeadersResponse (ic, ctx, index);\n\n\t\t\t\treturn await ParseHeadersAsync (stream, cancellationToken).ConfigureAwait (false);\n\t\t\t} finally {\n\t\t\t\tctx.Dispose ();\n\t\t\t}\n\t\t}\n\n\t\tImapCommand QueueGetHeadersCommand (int index, string partSpecifier, CancellationToken cancellationToken, ITransferProgress? progress, out FetchStreamContext ctx, out string[] tags)\n\t\t{\n\t\t\tif (index < 0 || index >= Count)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\tif (partSpecifier == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (partSpecifier));\n\n\t\t\tCheckState (true, false);\n\n\t\t\tvar command = string.Format (\"FETCH {0} ({1})\\r\\n\", index + 1, 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, int index, string[] tags)\n\t\t{","sourceCodeStart":3035,"sourceCodeEnd":3071,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L3035-L3071","documentation":"ArgumentOutOfRangeException (message is the parameter name \"index\") thrown by QueueGetHeadersCommand when the sequence index passed to GetHeaders(int index, ...) is negative or >= folder.Count. The IMAP FETCH command needs a 1-based message sequence number within the currently selected folder, so out-of-range values are rejected before any command is sent.","triggerScenarios":"Calling GetHeaders(int index, ...) with an index captured before messages were expunged, an index >= folder.Count, or a plain 0-based loop index passed to the API (which expects 0-based but bounded by Count).","commonSituations":"Iterating with `for (int i = 0; i <= folder.Count; i++)` (off-by-one); reusing indexes after a fetch summary refresh removed messages; using an index from a different, previously opened folder state.","solutions":["Validate `index >= 0 && index < folder.Count` immediately before calling GetHeaders.","Re-read folder.Count after any open/fetch/expunge, since the count changes as messages arrive or are removed.","Use MessageSummary-based workflows: fetch summaries first, then GetHeaders(summary.Index)."],"exampleFix":"// before\nfor (int i = 0; i <= folder.Count; i++)\n    Process(folder.GetHeaders(i));\n\n// after\nfor (int i = 0; i < folder.Count; i++)\n    Process(folder.GetHeaders(i));","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= folder.Count)\n    throw new ArgumentOutOfRangeException(nameof(index), \"Index must be within the current folder's message count.\");","typeGuard":"bool IsValidIndex(IMailFolder folder, int index) => (uint)index < (uint)folder.Count;","tryCatchPattern":"try { var headers = folder.GetHeaders(index, specifier); }\ncatch (ArgumentOutOfRangeException) { /* clamp to folder.Count - 1 or refresh folder */ }","preventionTips":["Always read folder.Count immediately before index-based calls.","Watch for off-by-one loops (`<=` vs `<`) over folder.Count.","Fetch summaries first and use summary.Index values instead of guessing indexes."],"tags":["imap","argument-out-of-range","mailkit","sequence-index"],"backgroundTag":"argument-out-of-range","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"}