jstedfast/MailKit · error · ArgumentNullException
part
Error message
part
What it means
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.
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.
Example fix
// before var part = summary.BodyParts.OfType<BodyPartText>().FirstOrDefault(); var stream = folder.GetStream(summary.UniqueId, part); // part may be null // after var part = summary.BodyParts.OfType<BodyPartText>().FirstOrDefault(); if (part == null) return; var stream = folder.GetStream(summary.UniqueId, part);
Defensive patterns
Strategy: validation
Validate before calling
if (bodyPart is null)
throw new InvalidOperationException("No body part resolved; fetch the body structure before calling GetStream."); Type guard
bool HasUsablePart([NotNullWhen(false)] BodyPart? part) => part != null;
Try / catch
try {
var stream = folder.GetStream(uid, bodyPart);
} catch (ArgumentNullException ex) when (ex.ParamName == "part") {
var structure = folder.GetBodyPart(uid, "");
stream = folder.GetStream(uid, structure.PartSpecifier);
} Prevention
- 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.
When it happens
Trigger: Calling MailFolder.GetStream(uid, null) — typically because a BodyPart lookup returned null or a variable was never assigned.
Common situations: 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.
Related errors
- ArgumentNullException
- ArgumentOutOfRangeException
- One or more of the messages is null.
- The number of messages and the number of flags must be…
- The uid is invalid.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/b3d30b44943df70e.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/MailFolder.cs:5976
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
/// <exception cref="ProtocolException">
/// The server's response contained unexpected tokens.
/// </exception>
/// <exception cref="CommandException">
/// The command failed.
/// </exception>
public virtual Stream GetStream (UniqueId uid, BodyPart part, CancellationToken cancellationToken = default, ITransferProgress? progress = null)
{
if (!uid.IsValid)
throw new ArgumentException ("The uid is invalid.", nameof (uid));
if (part == null)
throw new ArgumentNullException (nameof (part));
return GetStream (uid, part.PartSpecifier, cancellationToken, progress);
}
/// <summary>
/// Asynchronously get a body part as a stream.
/// </summary>
/// <remarks>
/// Asynchronously gets a body part as a stream.
/// </remarks>
/// <example>
/// <code language="c#" source="Examples\ImapBodyPartExamples.cs" region="GetBodyPartStreamsByUniqueId"/>
/// </example>
/// <returns>The body part stream.</returns>
/// <param name="uid">The UID of the message.</param>
/// <param name="part">The desired body part.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress reporting mechanism.</param>View on GitHub (pinned to 9d3859a785)