{"record":{"id":"93a3f32588686c13","repo":"jstedfast/MailKit","slug":"the-searchoptions-all-flag-cannot-be-combined-with-a-partial","errorCode":null,"errorMessage":"The SearchOptions.All flag cannot be combined with a partial range.","messagePattern":"The SearchOptions\\.All flag cannot be combined with a partial range\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":8639,"sourceCode":"\t\t/// <exception cref=\"FolderNotOpenException\">\n\t\t/// The <see cref=\"MailFolder\"/> is not currently open.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"CommandException\">\n\t\t/// The command failed.\n\t\t/// </exception>\n\t\tpublic virtual SearchResults Search (SearchOptions options, SearchQuery query, PartialRange partialRange, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif ((options & SearchOptions.All) != 0)\n\t\t\t\tthrow new ArgumentException (\"The SearchOptions.All flag cannot be combined with a partial range.\", nameof (options));\n\n\t\t\tif (query == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (query));\n\n\t\t\tthrow new NotSupportedException (\"The folder does not support partial searches.\");\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously search the folder for messages matching the specified query, returning only the specified range of results.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Asynchronously searches the folder for messages matching the specified query, returning only the\n\t\t/// search results within the specified range.</para>\n\t\t/// <para>Positive positions within the <paramref name=\"partialRange\"/> range are relative to the oldest matching\n\t\t/// message while negative positions are relative to the newest matching message. For example, a range of\n\t\t/// <c>1:500</c> will return the oldest 500 results while a range of <c>-1:-500</c> will return the newest\n\t\t/// 500 results.</para>\n\t\t/// <note type=\"note\">If the range specified by <paramref name=\"partialRange\"/> references results beyond the end","sourceCodeStart":8621,"sourceCodeEnd":8657,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L8621-L8657","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove SearchOptions.All from the options when a partial range is used, or drop the PartialRange - they are mutually exclusive","Use the simple Search(SearchQuery, CancellationToken) overload when a partial range is not needed","Call the search on an ImapMailFolder instance, since the base MailFolder.Search throws NotSupportedException for partial searches regardless of options"],"exampleFix":"// before\nvar results = folder.Search(SearchOptions.All, query, partialRange);\n// after\nvar results = folder.Search(SearchOptions.None, query, partialRange); // on ImapMailFolder","handlingStrategy":"validation","validationCode":"if ((options & SearchOptions.All) != 0 && partialRange != null)\n    throw new ArgumentException(\"SearchOptions.All cannot be combined with a partial range.\");","typeGuard":"static bool CanUsePartialRange(SearchOptions options) => (options & SearchOptions.All) == 0;","tryCatchPattern":"try {\n    var results = folder.Search(options, query, partialRange);\n} catch (ArgumentException ex) when (ex.Message.Contains(\"SearchOptions.All\")) {\n    // retry without SearchOptions.All or without the partial range\n} catch (NotSupportedException) {\n    // folder does not support partial searches; perform a full search instead\n}","preventionTips":["Treat SearchOptions.All and PartialRange as mutually exclusive","Only use the PartialRange overload on ImapMailFolder","Prefer the plain Search(query) overload unless paging is required"],"tags":["argument-validation","mailkit","search","conflicting-options"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}