{"record":{"id":"c66d7d006c5921ce","repo":"jstedfast/MailKit","slug":"the-uid-is-invalid","errorCode":null,"errorMessage":"The uid is invalid.","messagePattern":"The uid is invalid\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":5973,"sourceCode":"\t\t/// <exception cref=\"MessageNotFoundException\">\n\t\t/// The <see cref=\"IMailStore\"/> did not return the requested message stream.\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 Stream GetStream (UniqueId uid, BodyPart part, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\n\t\t{\n\t\t\tif (!uid.IsValid)\n\t\t\t\tthrow new ArgumentException (\"The uid is invalid.\", nameof (uid));\n\n\t\t\tif (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\n\n\t\t\treturn GetStream (uid, part.PartSpecifier, cancellationToken, progress);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously get a body part as a stream.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Asynchronously gets a body part as a stream.\n\t\t/// </remarks>\n\t\t/// <example>\n\t\t/// <code language=\"c#\" source=\"Examples\\ImapBodyPartExamples.cs\" region=\"GetBodyPartStreamsByUniqueId\"/>\n\t\t/// </example>\n\t\t/// <returns>The body part stream.</returns>\n\t\t/// <param name=\"uid\">The UID of the message.</param>","sourceCodeStart":5955,"sourceCodeEnd":5991,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L5955-L5991","documentation":"MailFolder.GetStream(UniqueId, BodyPart, ...) validates that the UniqueId passed for the message is a valid (non-zero, well-formed) UID before issuing the FETCH command. An invalid UniqueId means the value was never assigned from a message summary (its Id is 0 / IsValid is false), so the library throws ArgumentException instead of sending a meaningless IMAP command.","triggerScenarios":"Calling MailFolder.GetStream(uid, bodyPart) with a UniqueId that was default-constructed (UniqueId.Invalid, Id == 0) or copied from a source where it was never populated.","commonSituations":"Constructing `new UniqueId()` as a placeholder; deserializing message identifiers from storage where the UID was lost; fetching a BodyPart from a summary list but pairing it with an unrelated/empty uid variable.","solutions":["Populate the UniqueId from the message itself, e.g. folder.GetUID(index) or the Indexer/MessageSummary property (summary.UniqueId), before calling GetStream.","Check `uid.IsValid` before calling and skip or log the message if false.","If the message came from a body-part enumeration, keep the UniqueId from the same MessageSummary that produced the BodyPart.","When resuming saved state, persist and restore the UIDVALIDITY and UIDs; re-fetch the summary if UIDs cannot be trusted."],"exampleFix":"// before\nvar uid = new UniqueId();\nvar stream = folder.GetStream(uid, bodyPart);\n// after\nvar uid = folder.GetUID(index);\nif (!uid.IsValid) throw new InvalidOperationException($\"No UID for message at index {index}\");\nvar stream = folder.GetStream(uid, bodyPart);","handlingStrategy":"validation","validationCode":"if (uid == null || !uid.IsValid)\n    throw new InvalidOperationException(\"Cannot fetch stream: the UniqueId has not been populated (use summary.UniqueId or folder.GetUID(index)).\");","typeGuard":"bool IsUsableUid(UniqueId uid) => uid.IsValid;","tryCatchPattern":"try {\n    var stream = folder.GetStream(uid, bodyPart);\n} catch (ArgumentException ex) when (ex.ParamName == \"uid\") {\n    logger.LogWarning(ex, \"Invalid UID supplied; refetching summaries\");\n    summaries = folder.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure);\n}","preventionTips":["Always source UniqueId values from MessageSummary.UniqueId or folder.GetUID(index), never construct them manually.","Persist UIDVALIDITY alongside stored UIDs and discard them when UIDVALIDITY changes.","Check uid.IsValid before any uid-based folder call.","Never use `new UniqueId()` as a placeholder or default value."],"tags":["csharp","mailkit","argument-validation","imap"],"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-16T04:17:20.429Z"}