{"record":{"id":"501d652048d7255f","repo":"jstedfast/MailKit","slug":"specified-argument-was-out-of-the-range-of-valid-values-501d65","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'index')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'index'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/MessageEventArgs.cs","lineNumber":51,"sourceCode":"\t/// <remarks>\n\t/// Event args used when the state of a message changes.\n\t/// </remarks>\n\tpublic class MessageEventArgs : EventArgs\n\t{\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.MessageEventArgs\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"MessageEventArgs\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"index\">The message index.</param>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"index\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic MessageEventArgs (int index)\n\t\t{\n\t\t\tif (index < 0)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\tIndex = index;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.MessageEventArgs\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"MessageEventArgs\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"index\">The message index.</param>\n\t\t/// <param name=\"uid\">The unique id of the message.</param>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"index\"/> is out of range.\n\t\t/// </exception>\n\t\tpublic MessageEventArgs (int index, UniqueId uid)\n\t\t{\n\t\t\tif (index < 0)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MessageEventArgs.cs#L33-L69","documentation":"The MessageEventArgs(int index) constructor requires a non-negative message index. A ArgumentOutOfRangeException is thrown immediately when index < 0. Message indices in MailKit are zero-based, so negative values are never valid.","triggerScenarios":"Calling new MessageEventArgs(-1) directly, or passing an uninitialized/sentinel index (e.g. -1 meaning 'unknown') from code that raises message events for a folder.","commonSituations":"Custom folder adapters or IMAP event wrappers using -1 as a 'no index' sentinel, or an index computed from an empty/failed lookup.","solutions":["Pass a valid zero-based message index instead of a negative sentinel","Compute the index from the folder's message list rather than an unknown placeholder","Guard the index and skip raising the event when it is unknown"],"exampleFix":"// before\nvar args = new MessageEventArgs(unknownIndex); // -1\n// after\nif (unknownIndex >= 0) { var args = new MessageEventArgs(unknownIndex); }","handlingStrategy":"validation","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, \"Message index must be non-negative\");\nvar args = new MessageEventArgs(index);","typeGuard":"bool IsValidMessageIndex(int index) => index >= 0;","tryCatchPattern":"try { args = new MessageEventArgs(index); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\") { logger.LogWarning(\"Skipping event: invalid message index {Index}\", index); return; }","preventionTips":["Never use -1 as an 'unknown index' sentinel for event args","Resolve the real index via folder.IndexOf(message) before raising events","Use UID-based APIs when the sequence number is unknown"],"tags":["mailkit","argument-validation","events"],"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-23T08:17:48.524Z"}