jstedfast/MailKit · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values…

Error message

Specified argument was out of the range of valid values. (Parameter 'index')

What it means

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.

Solutions

  1. Pass a valid zero-based message index instead of a negative sentinel
  2. Compute the index from the folder's message list rather than an unknown placeholder
  3. Guard the index and skip raising the event when it is unknown

Example fix

// before
var args = new MessageEventArgs(unknownIndex); // -1
// after
if (unknownIndex >= 0) { var args = new MessageEventArgs(unknownIndex); }
Defensive patterns

Strategy: validation

Validate before calling

if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, "Message index must be non-negative");
var args = new MessageEventArgs(index);

Type guard

bool IsValidMessageIndex(int index) => index >= 0;

Try / catch

try { args = new MessageEventArgs(index); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "index") { logger.LogWarning("Skipping event: invalid message index {Index}", index); return; }

Prevention

When it happens

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

Common situations: Custom folder adapters or IMAP event wrappers using -1 as a 'no index' sentinel, or an index computed from an empty/failed lookup.

Related errors


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

Appendix: source

Thrown at MailKit/MessageEventArgs.cs:51

	/// <remarks>
	/// Event args used when the state of a message changes.
	/// </remarks>
	public class MessageEventArgs : EventArgs
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:MailKit.MessageEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MessageEventArgs"/>.
		/// </remarks>
		/// <param name="index">The message index.</param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.
		/// </exception>
		public MessageEventArgs (int index)
		{
			if (index < 0)
				throw new ArgumentOutOfRangeException (nameof (index));

			Index = index;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:MailKit.MessageEventArgs"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="MessageEventArgs"/>.
		/// </remarks>
		/// <param name="index">The message index.</param>
		/// <param name="uid">The unique id of the message.</param>
		/// <exception cref="System.ArgumentOutOfRangeException">
		/// <paramref name="index"/> is out of range.
		/// </exception>
		public MessageEventArgs (int index, UniqueId uid)
		{
			if (index < 0)

View on GitHub (pinned to 9d3859a785)