{"record":{"id":"770596ae4dfc8dfa","repo":"jstedfast/MailKit","slug":"one-or-more-of-the-uids-is-invalid","errorCode":null,"errorMessage":"One or more of the uids is invalid.","messagePattern":"One or more of the uids is invalid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/UniqueIdSet.cs","lineNumber":847,"sourceCode":"\t\t\t}\n\n\t\t\tif (uids is UniqueIdRange range) {\n\t\t\t\tyield return range.ToString ();\n\t\t\t\tyield break;\n\t\t\t}\n\n\t\t\tif (uids is UniqueIdSet set) {\n\t\t\t\tforeach (var subset in set.EnumerateSerializedSubsets (maxLength))\n\t\t\t\t\tyield return subset;\n\t\t\t\tyield break;\n\t\t\t}\n\n\t\t\tvar builder = new StringBuilder ();\n\t\t\tint index = 0;\n\n\t\t\twhile (index < uids.Count) {\n\t\t\t\tif (!uids[index].IsValid)\n\t\t\t\t\tthrow new ArgumentException (\"One or more of the uids is invalid.\", nameof (uids));\n\n\t\t\t\tuint start = uids[index].Id;\n\t\t\t\tuint end = uids[index].Id;\n\t\t\t\tint i = index + 1;\n\n\t\t\t\tif (i < uids.Count) {\n\t\t\t\t\tif (uids[i].Id == end + 1) {\n\t\t\t\t\t\tend = uids[i++].Id;\n\n\t\t\t\t\t\twhile (i < uids.Count && uids[i].Id == end + 1) {\n\t\t\t\t\t\t\tend++;\n\t\t\t\t\t\t\ti++;\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (uids[i].Id == end - 1) {\n\t\t\t\t\t\tend = uids[i++].Id;\n\n\t\t\t\t\t\twhile (i < uids.Count && uids[i].Id == end - 1) {\n\t\t\t\t\t\t\tend--;","sourceCodeStart":829,"sourceCodeEnd":865,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/UniqueIdSet.cs#L829-L865","documentation":"UniqueIdSet.ToString() builds a compact IMAP-style range string (e.g. \"1:5,8,12:20\") by serializing the UIDs it contains. Before serializing it validates every UID in the set; if any UniqueId is invalid (Id == 0, the sentinel for \"no uid\"), it throws this ArgumentException naming the uids parameter. An invalid UID cannot appear in an IMAP UID set, so the library refuses to produce a malformed sequence set.","triggerScenarios":"Calling ToString() on a UniqueIdSet that contains a UniqueId whose IsValid is false (UniqueId with Id 0). This typically happens when a UniqueId obtained from a failed or no-match message query (e.g. ImapFolder.GetUid(messageIndex) returning UniqueId.Invalid) is added to the set, then ToString() (directly or indirectly via APIs that build UID sequence sets) is called.","commonSituations":"Developers accumulate UIDs from message fetches where some lookups return UniqueId.Invalid, add them to a UniqueIdSet to build a FETCH or STORE command, and serialize it. Also common after downloading via POP or constructing UniqueId(0) by mistake as a 'default' value.","solutions":["Filter out invalid UIDs before adding them to the set: skip any uid where !uid.IsValid.","Check the source of each UniqueId (GetUid / GetMessageUid results) and handle UniqueId.Invalid at the call site instead of storing it.","Remove UniqueId.Invalid from an existing set before serializing (rebuild the set from a filtered collection).","Avoid using UniqueId(0) / UniqueId.Invalid as a default or sentinel value in your own collections that later get serialized."],"exampleFix":"// before\nvar set = new UniqueIdSet ();\nforeach (var msg in messages)\n    set.Add (folder.GetUid (msg.Index));\nvar query = set.ToString (); // throws if any uid is invalid\n\n// after\nvar set = new UniqueIdSet ();\nforeach (var msg in messages) {\n    var uid = folder.GetUid (msg.Index);\n    if (uid.IsValid)\n        set.Add (uid);\n}\nvar query = set.ToString ();","handlingStrategy":"validation","validationCode":"static bool AllValid (UniqueIdSet set)\n{\n    for (int i = 0; i < set.Count; i++)\n        if (!set[i].IsValid)\n            return false;\n    return true;\n}\n// call: if (AllValid (set)) var s = set.ToString ();","typeGuard":"static bool IsValidUid (UniqueId uid) => uid.IsValid && uid.Id != 0;","tryCatchPattern":"try {\n    var text = uidSet.ToString ();\n} catch (ArgumentException ex) {\n    // set contains UniqueId.Invalid; rebuild after filtering\n}","preventionTips":["Always check uid.IsValid (or uid != UniqueId.Invalid) before adding to a UniqueIdSet.","Never use UniqueId(0)/UniqueId.Invalid as a sentinel stored in collections that are later serialized.","Filter fetched UIDs at the boundary where messages are enumerated, not at serialization time."],"tags":["mailkit","imap","uid","argument-exception"],"backgroundTag":"invalid-argument-value","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"}