{"record":{"id":"5f0c7796485250fd","repo":"jstedfast/MailKit","slug":"part-imapfolderfetch","errorCode":null,"errorMessage":"part","messagePattern":"part","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapFolderFetch.cs","lineNumber":3248,"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=\"ImapProtocolException\">\n\t\t/// The server's response contained unexpected tokens.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ImapCommandException\">\n\t\t/// The server replied with a NO or BAD response.\n\t\t/// </exception>\n\t\tpublic override HeaderList GetHeaders (int index, BodyPart part, CancellationToken cancellationToken = default, ITransferProgress? progress = null)\n\t\t{\n\t\t\tif (index < 0 || index >= Count)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (index));\n\n\t\t\tif (part == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (part));\n\n\t\t\treturn GetHeaders (index, part.PartSpecifier, cancellationToken, progress);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously get the specified body part headers.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Gets the specified body part headers.\n\t\t/// </remarks>\n\t\t/// <returns>The body part headers.</returns>\n\t\t/// <param name=\"index\">The index of the message.</param>\n\t\t/// <param name=\"part\">The body part.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</param>\n\t\t/// <param name=\"progress\">The progress reporting mechanism.</param>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"index\"/> is out of range.\n\t\t/// </exception>","sourceCodeStart":3230,"sourceCodeEnd":3266,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapFolderFetch.cs#L3230-L3266","documentation":"ImapFolder.GetHeaders(index, part) throws ArgumentNullException when the BodyPart argument is null. The BodyPart supplies the PartSpecifier that identifies which MIME body section's headers to fetch, so a null part cannot produce a valid IMAP BODY section request. It is a standard guard against passing an unpopulated body-part reference.","triggerScenarios":"Calling ImapFolder.GetHeaders(index, null); passing a BodyPart variable that was never assigned from a folder.GetBodyPart/Query result (e.g. a deserialization or lookup returned null).","commonSituations":"Searching a parsed message tree for a part by content type and the search returns null; conditional logic that skips assigning the BodyPart on some code paths.","solutions":["Obtain the BodyPart from folder.GetBodyPart / ImapFolder.Query results before calling GetHeaders.","Null-check the BodyPart and fall back to fetching the envelope/headers via a BODY[] query.","Use the string overload GetHeaders(index, partSpecifier) when you only have a specifier like \"1.2\"."],"exampleFix":"// before\nvar headers = folder.GetHeaders(index, part);\n// after\nif (part is null)\n    throw new ArgumentException(\"A BodyPart is required to determine the MIME section specifier.\", nameof(part));\nvar headers = folder.GetHeaders(index, part);","handlingStrategy":"type-guard","validationCode":"if (part is null) throw new ArgumentException(\"part required\", nameof(part));","typeGuard":"bool HasSpecifier(BodyPart? part) => part is not null && part.PartSpecifier != null;","tryCatchPattern":"try { return folder.GetHeaders(index, part); }\ncatch (ArgumentNullException) { return null; }","preventionTips":["Resolve BodyPart from folder.GetBodyPart/Query results","Null-check lookup results before use","Use string specifier overloads when no BodyPart object exists"],"tags":["imap","null-argument","mailkit","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-15T23:17:13.987Z"}