{"record":{"id":"20af95d608d6e994","repo":"jstedfast/MailKit","slug":"one-or-more-of-the-indexes-are-invalid","errorCode":null,"errorMessage":"One or more of the indexes are invalid.","messagePattern":"One or more of the indexes are invalid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Pop3/Pop3Client.cs","lineNumber":2587,"sourceCode":"\t\t\tif (index < 0 || index >= total)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\t\t}\n\n\t\tbool CheckCanDownload (IList<int> indexes)\n\t\t{\n\t\t\tCheckDisposed ();\n\t\t\tCheckConnected ();\n\t\t\tCheckAuthenticated ();\n\n\t\t\tif (indexes == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (indexes));\n\n\t\t\tif (indexes.Count == 0)\n\t\t\t\treturn false;\n\n\t\t\tfor (int i = 0; i < indexes.Count; i++) {\n\t\t\t\tif (indexes[i] < 0 || indexes[i] >= total)\n\t\t\t\t\tthrow new ArgumentException (\"One or more of the indexes are invalid.\", nameof (indexes));\n\t\t\t}\n\n\t\t\treturn true;\n\t\t}\n\n\t\tbool CheckCanDownload (int startIndex, int count)\n\t\t{\n\t\t\tCheckDisposed ();\n\t\t\tCheckConnected ();\n\t\t\tCheckAuthenticated ();\n\n\t\t\tif (startIndex < 0 || startIndex >= total)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (startIndex));\n\n\t\t\tif (count < 0 || count > (total - startIndex))\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (count));\n\n\t\t\treturn count > 0;","sourceCodeStart":2569,"sourceCodeEnd":2605,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Pop3/Pop3Client.cs#L2569-L2605","documentation":"Pop3Client validates message index lists before issuing multi-message commands (e.g. downloading several messages). Every index must be a 0-based sequence number within the current message count; any index that is negative or >= total messages causes this ArgumentException. It is an argument validation guard, not a network failure.","triggerScenarios":"Calling DownloadMessages/DownloadBody/etc. with an IList<int> containing an index >= the value of Pop3Client.Count or a negative number; using 1-based message numbers from a UIDL listing as 0-based indexes; reusing cached indexes after messages were deleted.","commonSituations":"Iterating `for (i = 1; i <= client.Count; i++)` (off-by-one) instead of `i < client.Count`; using message numbers printed by the server (1-based) directly; deleting messages mid-loop and then retrying stale indexes.","solutions":["Clamp/check each index: only pass values where 0 <= index < client.Count.","If you have 1-based message numbers, subtract 1 before passing them.","Re-read client.Count after any DeleteMessages call and refresh your index list.","Verify you are connected (client.IsConnected and authenticated) so Count reflects the real mailbox."],"exampleFix":"// before\nvar indexes = new List<int>();\nfor (int i = 1; i <= client.Count; i++) indexes.Add(i); // off-by-one: i == Count is invalid\nclient.DownloadMessages(indexes);\n// after\nvar indexes = Enumerable.Range(0, client.Count).ToList();\nclient.DownloadMessages(indexes);","handlingStrategy":"validation","validationCode":"// before calling the API\nif (indexes.Any(i => i < 0 || i >= client.Count))\n    throw new InvalidOperationException(\"message index out of range\");","typeGuard":"bool IsValidIndex(int i, int total) => i >= 0 && i < total;","tryCatchPattern":"try {\n    client.DownloadMessages(indexes);\n} catch (ArgumentException ex) when (ex.ParamName == \"indexes\") {\n    // log and rebuild indexes from client.Count\n}","preventionTips":["Always derive indexes from client.Count at call time, never cache them","Remember MailKit POP3 indexes are 0-based; server message numbers are 1-based","Refresh index lists after any DeleteMessages call"],"tags":["pop3","argument-validation","index-out-of-range","mailkit"],"backgroundTag":"index-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"}