jstedfast/MailKit · error · ArgumentNullException
contentType
Error message
contentType
What it means
The protected BodyPart(ContentType, string) constructor throws ArgumentNullException when the contentType parameter is null. MailKit requires every BODYSTRUCTURE part to carry a parsed MIME ContentType, so a null value would leave the part in an invalid state. The parameter name is reported via nameof, which is why the message is exactly "contentType".
Solutions
- Pass a valid parsed ContentType, e.g. new ContentType("text", "plain"), instead of null
- If the source MIME part had no Content-Type header, default to new ContentType("text","plain") per RFC 2045
- Guard the value before invoking the constructor and throw a more descriptive error
Example fix
// before
var part = new CustomBodyPart(contentType, "1"); // contentType was null
// after
contentType ??= new ContentType("text", "plain");
var part = new CustomBodyPart(contentType, "1"); Defensive patterns
Strategy: validation
Validate before calling
if (contentType == null) throw new ArgumentException("A parsed ContentType is required; use new ContentType(\"text\", \"plain\") when the MIME header is missing.", nameof(contentType)); Type guard
static bool HasContentType(ContentType? ct) => ct is not null;
Try / catch
try { var part = new CustomBodyPart(contentType, specifier); }
catch (ArgumentNullException ex) when (ex.ParamName == "contentType") { /* supply default ContentType and retry */ } Prevention
- Default missing MIME Content-Type headers to text/plain per RFC 2045 before constructing parts
- Use nameof-based null checks in subclass constructors matching the base class contract
- Never pass nullable values straight into BodyPart constructors
When it happens
Trigger: Calling the protected constructor from a derived BodyPart subclass and passing a null ContentType — e.g. new MyBodyPart(null, "1").
Common situations: Custom subclassing of BodyPart when the MIME Content-Type header was missing or failed to parse and the caller passes null instead of a default like text/plain; refactoring code that previously accepted a nullable content type.
Related errors
- partSpecifier
- visitor
- part
- Annotation entry paths must not include a part-specifier.
- Invalid annotation entry path.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/9f493099e6cd345e.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/BodyPart.cs:79
}
/// <summary>
/// Initializes a new instance of the <see cref="MailKit.BodyPart"/> class.
/// </summary>
/// <remarks>
/// Creates a new <see cref="MailKit.BodyPart"/>.
/// </remarks>
/// <param name="contentType">The content type.</param>
/// <param name="partSpecifier">The part specifier.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="contentType"/> is <see langword="null" />.</para>
/// <para>-or-</para>
/// <para><paramref name="partSpecifier"/> is <see langword="null" />.</para>
/// </exception>
protected BodyPart (ContentType contentType, string partSpecifier)
{
if (contentType == null)
throw new ArgumentNullException (nameof (contentType));
if (partSpecifier == null)
throw new ArgumentNullException (nameof (partSpecifier));
ContentType = contentType;
PartSpecifier = partSpecifier;
}
/// <summary>
/// Gets the Content-Type of the body part.
/// </summary>
/// <remarks>
/// Gets the Content-Type of the body part.
/// </remarks>
/// <value>The content type.</value>
public ContentType ContentType {
get; set;
}View on GitHub (pinned to 9d3859a785)