{"record":{"id":"e0ca2e30b525e409","repo":"jstedfast/MailKit","slug":"part","errorCode":null,"errorMessage":"part","messagePattern":"part","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/BodyPartCollection.cs","lineNumber":90,"sourceCode":"\t\t/// <value><see langword=\"true\" /> if this collection is read only; otherwise, <see langword=\"false\" />.</value>\n\t\tpublic bool IsReadOnly {\n\t\t\tget { return false; }\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Add the specified body part to the collection.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Adds the specified body part to the collection.\n\t\t/// </remarks>\n\t\t/// <param name=\"part\">The body part.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"part\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tpublic void Add (BodyPart part)\n\t\t{\n\t\t\tif (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\n\n\t\t\tcollection.Add (part);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Clears the body part collection.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Removes all of the body parts from the collection.\n\t\t/// </remarks>\n\t\tpublic void Clear ()\n\t\t{\n\t\t\tcollection.Clear ();\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Checks if the collection contains the specified body part.\n\t\t/// </summary>","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/BodyPartCollection.cs#L72-L108","documentation":"BodyPartCollection.Add(BodyPart) throws ArgumentNullException when the part parameter is null. Collections of BODYSTRUCTURE parts must only contain valid BodyPart instances; adding null would corrupt traversal and indexing of the MIME tree. The message is the parameter name \"part\".","triggerScenarios":"Calling bodyParts.Add(null) directly, or indirectly from MailKit's internal ParseMultipart/ParseMultipartAsync when building the parsed BODYSTRUCTURE tree for a multipart message.","commonSituations":"Programmatically constructing a BodyPart tree where a sub-part failed to parse and null was added instead of being skipped; code that maps over a nullable list of parts without filtering nulls.","solutions":["Filter null entries before adding: if (part != null) collection.Add(part)","Use AddRange on a pre-filtered sequence (parts.Where(p => p != null))","Fix the upstream parsing step so it never yields null parts"],"exampleFix":"// before\nforeach (var p in parts) multipartBodyParts.Add(p); // some p were null\n// after\nforeach (var p in parts)\n\tif (p != null) multipartBodyParts.Add(p);","handlingStrategy":"validation","validationCode":"if (part == null) return; // or throw with a descriptive message before calling Add","typeGuard":"static bool IsAddable(BodyPart? p) => p is not null;","tryCatchPattern":"try { collection.Add(part); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"part\") { /* log and skip the null part */ }","preventionTips":["Filter nulls with LINQ before bulk-adding parts: parts.Where(p => p != null)","Ensure parsing code returns empty collections or skips entries rather than null parts","Validate programmatically built MIME trees with null checks before assembly"],"tags":["mailkit","null-argument","collection","mime"],"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"}