jstedfast/MailKit · error · ArgumentException

One or more of the indexes is negative.

Error message

One or more of the indexes is negative.

What it means

ImapUtils.FormatIndexSet validates each entry of the index list while compressing it into an IMAP sequence set; any negative value makes the sequence set invalid, so it throws an ArgumentException naming the 'indexes' parameter. Message sequence numbers and UIDs are always positive.

Solutions

  1. Validate/filters the list before calling: remove or reject entries where i < 0.
  2. Ensure sequence numbers are converted to 1-based (index + 1) when derived from zero-based positions.
  3. Replace -1 sentinels with a nullable type or explicit skip before building the call.

Example fix

// before
foreach (var idx in rows) indexes.Add(idx.Position - 1);
await folder.AddFlagsAsync(indexes, MessageFlags.Seen, true);

// after
foreach (var idx in rows) indexes.Add(idx.Position); // keep 1-based
if (indexes.Any(i => i < 0))
    throw new InvalidOperationException("negative index in input");
await folder.AddFlagsAsync(indexes, MessageFlags.Seen, true);
Defensive patterns

Strategy: validation

Validate before calling

if (indexes.Any(i => i < 0))
    throw new ArgumentException("indexes must be positive IMAP sequence numbers", nameof(indexes));

Prevention

When it happens

Trigger: Calling GetMessages/GetUids/flag methods with a list containing a negative int (e.g. an uninitialized variable, an arithmetic result, or a 1-based index computed as messageIndex - 1 when messageIndex is 0).

Common situations: Off-by-one conversions between 0-based list indices and 1-based IMAP sequence numbers; sentinel values like -1 meaning 'none' passed through unfiltered; data parsed from external sources containing negatives.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapUtils.cs:280

		public static void FormatIndexSet (ImapEngine engine, StringBuilder builder, IList<int> indexes)
		{
			if (engine == null)
				throw new ArgumentNullException (nameof (engine));

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

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

			if (indexes.Count == 0)
				throw new ArgumentException ("No indexes were specified.", nameof (indexes));

			int index = 0;

			while (index < indexes.Count) {
				if (indexes[index] < 0)
					throw new ArgumentException ("One or more of the indexes is negative.", nameof (indexes));

				int begin = indexes[index];
				int end = indexes[index];
				int i = index + 1;

				if (i < indexes.Count) {
					if (indexes[i] == end + 1) {
						end = indexes[i++];

						while (i < indexes.Count && indexes[i] == end + 1) {
							end++;
							i++;
						}
					} else if (indexes[i] == end - 1 && engine.QuirksMode != ImapQuirksMode.hMailServer) {
						end = indexes[i++];

						while (i < indexes.Count && indexes[i] == end - 1) {
							end--;

View on GitHub (pinned to 9d3859a785)