{"record":{"id":"b3d30b44943df70e","repo":"jstedfast/MailKit","slug":"part-mailfolder","errorCode":null,"errorMessage":"part","messagePattern":"part","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/MailFolder.cs","lineNumber":5976,"sourceCode":"\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>\n\t\t/// <param name=\"part\">The desired body part.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <param name=\"progress\">The progress reporting mechanism.</param>","sourceCodeStart":5958,"sourceCodeEnd":5994,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailFolder.cs#L5958-L5994","documentation":"MailFolder.GetStream(UniqueId, BodyPart, ...) throws ArgumentNullException with param name \"part\" when the BodyPart argument is null. The library requires a non-null BodyPart to know which MIME part specifier to fetch.","triggerScenarios":"Calling MailFolder.GetStream(uid, null) — typically because a BodyPart lookup returned null or a variable was never assigned.","commonSituations":"Searching a BodyPart tree (e.g. BodyParts.OfType or FirstOrDefault) for a text/html part that does not exist and passing the null result straight to GetStream; refactoring where the part argument was dropped.","solutions":["Ensure the BodyPart comes from a fetched BodyStructure (folder.GetBodyPart / message summary BodyParts) before passing it.","Null-check the result of any LINQ lookup (FirstOrDefault/SingleOrDefault) before calling GetStream.","Fall back to a default part such as the message body via GetStream(uid, \"1\") or GetMessage when the desired part is absent."],"exampleFix":"// before\nvar part = summary.BodyParts.OfType<BodyPartText>().FirstOrDefault();\nvar stream = folder.GetStream(summary.UniqueId, part); // part may be null\n// after\nvar part = summary.BodyParts.OfType<BodyPartText>().FirstOrDefault();\nif (part == null) return;\nvar stream = folder.GetStream(summary.UniqueId, part);","handlingStrategy":"validation","validationCode":"if (bodyPart is null)\n    throw new InvalidOperationException(\"No body part resolved; fetch the body structure before calling GetStream.\");","typeGuard":"bool HasUsablePart([NotNullWhen(false)] BodyPart? part) => part != null;","tryCatchPattern":"try {\n    var stream = folder.GetStream(uid, bodyPart);\n} catch (ArgumentNullException ex) when (ex.ParamName == \"part\") {\n    var structure = folder.GetBodyPart(uid, \"\");\n    stream = folder.GetStream(uid, structure.PartSpecifier);\n}","preventionTips":["Fetch the body structure (GetBodyPart or Fetch with BodyStructure) before enumerating parts.","Treat FirstOrDefault part lookups as nullable and check before use.","Enable nullable reference types to surface unassigned BodyPart flows at compile time.","Fall back to GetMessage when no specific part matches."],"tags":["csharp","mailkit","null-argument","argument-validation"],"backgroundTag":"null-argument","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"}