jstedfast/MailKit · error · ArgumentException

The SearchOptions.All flag cannot be combined with a…

Error message

The SearchOptions.All flag cannot be combined with a partial range.

What it means

MailKit throws this ArgumentException from the synchronous ImapFolder.Search overload when SearchOptions.All is combined with a PartialRange. The options are mutually exclusive: SearchOptions.All means 'return every match with minimal data', while a partial range restricts results to a window — the semantics conflict, so the library rejects it up front.

Solutions

  1. Remove SearchOptions.All from the options mask when passing a PartialRange.
  2. Use SearchOptions.None (or ESEARCH-compatible options like Count/Final) together with the partial range.
  3. If you truly want every result, pass null/no partial range instead and keep SearchOptions.All.

Example fix

// before
var results = folder.Search (SearchOptions.All, query, new PartialRange (0, 10));

// after
var results = folder.Search (SearchOptions.None, query, new PartialRange (0, 10));
Defensive patterns

Strategy: validation

Validate before calling

if (options.HasFlag (SearchOptions.All) && partialRange != null)
    throw new ArgumentException ("SearchOptions.All cannot be combined with a partial range", nameof (options));

Try / catch

try {
    results = folder.Search (options, query, partialRange);
} catch (ArgumentException ex) when (ex.ParamName == "options") {
    results = folder.Search (options & ~SearchOptions.All, query, partialRange);
}

Prevention

When it happens

Trigger: Calling folder.Search(SearchOptions.All, query, partialRange) with any PartialRange value. The overload validation happens before any network I/O.

Common situations: Combining flags bitwise out of habit (SearchOptions.All | SearchOptions.Count) and then adding paging; copying a Search call that worked without a range and adding a PartialRange parameter without dropping the All flag.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Imap/ImapFolderSearch.cs:1321

		/// <exception cref="FolderNotOpenException">
		/// The <see cref="ImapFolder"/> is not currently open.
		/// </exception>
		/// <exception cref="System.OperationCanceledException">
		/// The operation was canceled via the cancellation token.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ImapProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="ImapCommandException">
		/// The server replied with a NO or BAD response.
		/// </exception>
		public override SearchResults Search (SearchOptions options, SearchQuery query, PartialRange partialRange, CancellationToken cancellationToken = default)
		{
			if ((options & SearchOptions.All) != 0)
				throw new ArgumentException ("The SearchOptions.All flag cannot be combined with a partial range.", nameof (options));

			return Search (options, query, partialRange, true, cancellationToken);
		}

		/// <summary>
		/// Asynchronously search the folder for messages matching the specified query, returning only the specified range of results.
		/// </summary>
		/// <remarks>
		/// <para>Asynchronously searches the folder for messages matching the specified query, returning only the
		/// search results within the specified range.</para>
		/// <para>Positive positions within the <paramref name="partialRange"/> range are relative to the oldest matching
		/// message while negative positions are relative to the newest matching message. For example, a range of
		/// <c>1:500</c> will return the oldest 500 results while a range of <c>-1:-500</c> will return the newest
		/// 500 results.</para>
		/// <note type="note">If the range specified by <paramref name="partialRange"/> references results beyond the end
		/// of the complete set of matching messages, then the results will only contain the unique identifiers that
		/// fall within the range (if any).</note>
		/// </remarks>

View on GitHub (pinned to 9d3859a785)