{"record":{"id":"796d66082415eaac","repo":"jstedfast/MailKit","slug":"index-mailfolder","errorCode":null,"errorMessage":"index","messagePattern":"index","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":4401,"sourceCode":"\t\t/// <exception cref=\"FolderNotOpenException\">\n\t\t/// The folder is not currently open in read-write mode.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"CommandException\">\n\t\t/// The command failed.\n\t\t/// </exception>\n\t\tpublic virtual Task MoveToAsync (int index, IMailFolder destination, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (index < 0 || index >= Count)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\treturn MoveToAsync (new [] { index }, destination, cancellationToken);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Move the specified messages to the destination folder.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Moves the specified messages to the destination folder.\n\t\t/// </remarks>\n\t\t/// <param name=\"indexes\">The indexes of the messages to move.</param>\n\t\t/// <param name=\"destination\">The destination folder.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <para><paramref name=\"indexes\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"destination\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>","sourceCodeStart":4383,"sourceCodeEnd":4419,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L4383-L4419","documentation":"MailFolder.MoveToAsync (int index, ...) validates that the message index is within [0, Count) before delegating to the multi-message MoveToAsync. An out-of-range index throws ArgumentOutOfRangeException for 'index'. MailKit throws this rather than letting an invalid UID/index reach the IMAP server.","triggerScenarios":"Calling folder.MoveToAsync (i, destination) where i < 0 or i >= folder.Count — e.g. a stale index after messages were expunged, or indexing past the end of the folder.","commonSituations":"Caching indexes from a previous session while other clients expunged messages; iterating with an old Count; confusing message indexes with UIDs; using an index from a different folder.","solutions":["Check the index against the current folder.Count before calling (and re-open/Refresh the folder if it may be stale).","Prefer MoveToAsync (IList<int> ids, ...) only after re-validating against the live folder, or use UIDs via UniqueId-based move APIs to avoid index staleness.","Catch ArgumentOutOfRangeException and treat it as 'message no longer exists', then refresh the folder summary.","After expunges by any client, call folder.Refresh (folder.Summary, cancellationToken) to update Count."],"exampleFix":"// before\nawait folder.MoveToAsync (index, trash); // index may be stale\n\n// after\nif (index >= 0 && index < folder.Count)\n    await folder.MoveToAsync (index, trash);\nelse {\n    await folder.CloseAsync (false, ct);\n    await folder.OpenAsync (FolderAccess.ReadWrite, ct); // refresh\n}","handlingStrategy":"validation","validationCode":"if (folder.IsOpen == false)\n    await folder.OpenAsync (FolderAccess.ReadWrite);\nif (index < 0 || index >= folder.Count)\n    return; // or refresh and re-map index","typeGuard":"bool ValidIndex (IMailFolder folder, int index) => index >= 0 && index < folder.Count;","tryCatchPattern":"try {\n    await folder.MoveToAsync (index, trash, cancellationToken);\n} catch (ArgumentOutOfRangeException) {\n    await folder.CloseAsync (false, cancellationToken);\n    await folder.OpenAsync (FolderAccess.ReadWrite, cancellationToken);\n    // re-map or skip: the message was likely expunged\n}","preventionTips":["Re-open or Refresh the folder after other clients may have expunged messages.","Prefer UniqueId-based move APIs over sequence indexes to avoid stale indexes.","Never cache message indexes across sessions or long-running operations.","Iterate using live folder.Count inside loops, descending when removing/moving."],"tags":["argument-out-of-range","imap-move","index-stale","mailkit"],"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-16T04:17:20.429Z"}