{"record":{"id":"9b8d0a4c6fad3414","repo":"jstedfast/MailKit","slug":"cannot-search-for-an-invalid-unique-identifier","errorCode":null,"errorMessage":"Cannot search for an invalid unique identifier.","messagePattern":"Cannot search for an invalid unique identifier\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Search/UidSearchQuery.cs","lineNumber":77,"sourceCode":"\t\t\t\tthrow new ArgumentException (\"Cannot search for an empty set of unique identifiers.\", nameof (uids));\n\n\t\t\tUids = uids;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.Search.UidSearchQuery\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new unique identifier-based search query.\n\t\t/// </remarks>\n\t\t/// <param name=\"uid\">The unique identifier to match against.</param>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <paramref name=\"uid\"/> is an invalid unique identifier.\n\t\t/// </exception>\n\t\tpublic UidSearchQuery (UniqueId uid) : base (SearchTerm.Uid)\n\t\t{\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"Cannot search for an invalid unique identifier.\", nameof (uid));\n\n\t\t\tUids = new UniqueIdSet (SortOrder.Ascending) {\n\t\t\t\tuid\n\t\t\t};\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Gets the unique identifiers to match against.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the unique identifiers to match against.\n\t\t/// </remarks>\n\t\t/// <value>The unique identifiers.</value>\n\t\tpublic new IList<UniqueId> Uids {\n\t\t\tget; private set;\n\t\t}\n\t}\n}","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Search/UidSearchQuery.cs#L59-L95","documentation":"The UidSearchQuery(UniqueId) constructor requires a valid UID; MailKit represents an invalid/unassigned UID (Id == 0) with UniqueId.IsValid == false. Searching for such a UID cannot be translated to an IMAP UID SEARCH command, so the constructor throws ArgumentException immediately.","triggerScenarios":"Calling new UidSearchQuery(uid) where uid.IsValid is false — typically a default(UniqueId) (Id 0, Validity 0) or a UID obtained from a failed/uninitialized operation.","commonSituations":"Passing UniqueId.MinValue or default(UniqueId) obtained from an uninitialized field; mapping code that stores UIDs in a DB and reads back zero/invalid placeholders; using UIDs parsed from user input that was never validated.","solutions":["Check uid.IsValid before constructing the query and skip or handle invalid UIDs separately.","Verify the code path that produced the UniqueId actually assigns it from a fetched message (e.g., message.UniqueId from Fetch(UniqueId, ...)).","For persisted UIDs, validate that stored Id/Validity values are non-zero before rebuilding a UniqueId."],"exampleFix":"// before\nfolder.Search(new UidSearchQuery(uid)); // throws if uid invalid\n\n// after\nif (uid.IsValid)\n    folder.Search(new UidSearchQuery(uid));","handlingStrategy":"validation","validationCode":"if (uid.IsValid)\n    folder.Search(SearchQuery.Uids(uid));","typeGuard":"bool IsValidUid(UniqueId uid) => uid != default && uid.IsValid;","tryCatchPattern":"try {\n    folder.Search(new UidSearchQuery(uid));\n} catch (ArgumentException ex) when (ex.ParamName == \"uid\") {\n    // invalid UID: log and skip, or re-fetch the message to obtain a valid UID\n}","preventionTips":["Never persist or pass around default(UniqueId); obtain UIDs from Fetch/Search results only.","Filter persisted UID lists with IsValid before reconstructing queries.","Validate user-supplied numeric UIDs (non-zero) before wrapping them in UniqueId."],"tags":["mailkit","imap","argument-validation","uniqueid"],"backgroundTag":"invalid-identifier","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}