jstedfast/MailKit · error · ArgumentException

One or more of the indexes are invalid.

Error message

One or more of the indexes are invalid.

What it means

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.

Solutions

  1. Clamp/check each index: only pass values where 0 <= index < client.Count.
  2. If you have 1-based message numbers, subtract 1 before passing them.
  3. Re-read client.Count after any DeleteMessages call and refresh your index list.
  4. Verify you are connected (client.IsConnected and authenticated) so Count reflects the real mailbox.

Example fix

// before
var indexes = new List<int>();
for (int i = 1; i <= client.Count; i++) indexes.Add(i); // off-by-one: i == Count is invalid
client.DownloadMessages(indexes);
// after
var indexes = Enumerable.Range(0, client.Count).ToList();
client.DownloadMessages(indexes);
Defensive patterns

Strategy: validation

Validate before calling

// before calling the API
if (indexes.Any(i => i < 0 || i >= client.Count))
    throw new InvalidOperationException("message index out of range");

Type guard

bool IsValidIndex(int i, int total) => i >= 0 && i < total;

Try / catch

try {
    client.DownloadMessages(indexes);
} catch (ArgumentException ex) when (ex.ParamName == "indexes") {
    // log and rebuild indexes from client.Count
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/20af95d608d6e994. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Net/Pop3/Pop3Client.cs:2587

			if (index < 0 || index >= total)
				throw new ArgumentOutOfRangeException (nameof (index));
		}

		bool CheckCanDownload (IList<int> indexes)
		{
			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			if (indexes == null)
				throw new ArgumentNullException (nameof (indexes));

			if (indexes.Count == 0)
				return false;

			for (int i = 0; i < indexes.Count; i++) {
				if (indexes[i] < 0 || indexes[i] >= total)
					throw new ArgumentException ("One or more of the indexes are invalid.", nameof (indexes));
			}

			return true;
		}

		bool CheckCanDownload (int startIndex, int count)
		{
			CheckDisposed ();
			CheckConnected ();
			CheckAuthenticated ();

			if (startIndex < 0 || startIndex >= total)
				throw new ArgumentOutOfRangeException (nameof (startIndex));

			if (count < 0 || count > (total - startIndex))
				throw new ArgumentOutOfRangeException (nameof (count));

			return count > 0;

View on GitHub (pinned to 9d3859a785)