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

MailFolder.Search(SearchOptions options, SearchQuery query, PartialRange partialRange, ...) throws ArgumentException stating that SearchOptions.All cannot be combined with a partial range. SearchOptions.All is a directive that the search return all matching messages regardless of the default result window, which is semantically incompatible with a partial (paged) range; the base MailFolder implementation additionally always throws NotSupportedException since partial searches are only supported by folders whose protocol (IMAP) allows them.

Solutions

  1. Remove SearchOptions.All from the options when a partial range is used, or drop the PartialRange - they are mutually exclusive
  2. Use the simple Search(SearchQuery, CancellationToken) overload when a partial range is not needed
  3. Call the search on an ImapMailFolder instance, since the base MailFolder.Search throws NotSupportedException for partial searches regardless of options

Example fix

// before
var results = folder.Search(SearchOptions.All, query, partialRange);
// after
var results = folder.Search(SearchOptions.None, query, partialRange); // on ImapMailFolder
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool CanUsePartialRange(SearchOptions options) => (options & SearchOptions.All) == 0;

Try / catch

try {
    var results = folder.Search(options, query, partialRange);
} catch (ArgumentException ex) when (ex.Message.Contains("SearchOptions.All")) {
    // retry without SearchOptions.All or without the partial range
} catch (NotSupportedException) {
    // folder does not support partial searches; perform a full search instead
}

Prevention

When it happens

Trigger: Calling folder.Search(SearchOptions.All, query, partialRange) on a base MailFolder or an IMAP folder while requesting both the All option and a PartialRange. Also occurs when constructing a PartialRange unnecessarily - the 2-argument Search overload should be used instead.

Common situations: Migrating code that previously used SearchOptions.All for completeness checks and then adding paging with PartialRange; assuming the default MailFolder implementation supports partial searches when only ImapMailFolder does.

Related errors


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

Appendix: source

Thrown at MailKit/MailFolder.cs:8639

		/// <exception cref="FolderNotOpenException">
		/// The <see cref="MailFolder"/> 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="ProtocolException">
		/// The server's response contained unexpected tokens.
		/// </exception>
		/// <exception cref="CommandException">
		/// The command failed.
		/// </exception>
		public virtual 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));

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

			throw new NotSupportedException ("The folder does not support partial searches.");
		}

		/// <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

View on GitHub (pinned to 9d3859a785)