{"record":{"id":"83b0374783407d53","repo":"jstedfast/MailKit","slug":"the-searchoptions-all-flag-cannot-be-combined-with-a-partial-83b037","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":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderSearch.cs","lineNumber":1321,"sourceCode":"\t\t/// <exception cref=\"FolderNotOpenException\">\n\t\t/// The <see cref=\"ImapFolder\"/> 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=\"ImapProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override 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\treturn Search (options, query, partialRange, true, cancellationToken);\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\n\t\t/// of the complete set of matching messages, then the results will only contain the unique identifiers that\n\t\t/// fall within the range (if any).</note>\n\t\t/// </remarks>","sourceCodeStart":1303,"sourceCodeEnd":1339,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderSearch.cs#L1303-L1339","documentation":"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.","triggerScenarios":"Calling folder.Search(SearchOptions.All, query, partialRange) with any PartialRange value. The overload validation happens before any network I/O.","commonSituations":"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.","solutions":["Remove SearchOptions.All from the options mask when passing a PartialRange.","Use SearchOptions.None (or ESEARCH-compatible options like Count/Final) together with the partial range.","If you truly want every result, pass null/no partial range instead and keep SearchOptions.All."],"exampleFix":"// before\nvar results = folder.Search (SearchOptions.All, query, new PartialRange (0, 10));\n\n// after\nvar results = folder.Search (SearchOptions.None, query, new PartialRange (0, 10));","handlingStrategy":"validation","validationCode":"if (options.HasFlag (SearchOptions.All) && partialRange != null)\n    throw new ArgumentException (\"SearchOptions.All cannot be combined with a partial range\", nameof (options));","typeGuard":null,"tryCatchPattern":"try {\n    results = folder.Search (options, query, partialRange);\n} catch (ArgumentException ex) when (ex.ParamName == \"options\") {\n    results = folder.Search (options & ~SearchOptions.All, query, partialRange);\n}","preventionTips":["Centralize option-mask construction in one helper that enforces the All/partial-range exclusivity.","Avoid mixing SearchOptions.All with paging features by design; pick one retrieval mode.","Add unit tests covering invalid option combinations."],"tags":["imap","mailkit","argument-error","search-options","mutually-exclusive"],"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-16T04:17:20.429Z"}