jstedfast/MailKit · error · ArgumentNullException
text
Error message
text
What it means
The static BodyPart.TryParse(string, out BodyPart?) throws ArgumentNullException when the text parameter is null. TryParse is designed to return false for malformed BODYSTRUCTURE strings, but a null input is treated as a programming error rather than a parse failure. The message is the parameter name "text".
Solutions
- Check for null/empty before calling TryParse and skip parsing when absent
- Re-fetch the summary with BodyStructure summary items requested (MessageSummaryItems.BodyStructure)
- Coalesce null to string.Empty only if an empty parse result is acceptable
Example fix
// before
BodyPart.TryParse(bodyStructureText, out var part); // text was null
// after
if (bodyStructureText != null && BodyPart.TryParse(bodyStructureText, out var part)) { ... } Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(bodyStructureText)) return; // skip parse when the server returned no BODYSTRUCTURE
Type guard
static bool CanParse(string? text) => !string.IsNullOrEmpty(text);
Try / catch
try { ok = BodyPart.TryParse(text, out var part); }
catch (ArgumentNullException ex) when (ex.ParamName == "text") { part = null; ok = false; } Prevention
- Request MessageSummaryItems.BodyStructure when fetching ImapMessageSummary items so the text is populated
- Null-check strings obtained from caches, JSON, or network layers before parsing
- Prefer TryParse's false return for malformed input; reserve null checks for truly absent data
When it happens
Trigger: Calling BodyPart.TryParse(null, out var part), typically with a string obtained from an IMAP FETCH BODYSTRUCTURE response that was never assigned or was null in the ImapMessageSummary.
Common situations: Parsing a cached/remote BODYSTRUCTURE value that is null when the server omitted it (e.g. some responses for message/rfc822 or when fetching summary items without BODYSTRUCTURE); deserializing summaries from JSON where the field came back null.
Related errors
- Value cannot be null. (Parameter 'entry')
- partSpecifier
- Value cannot be null. (Parameter 'name')
- Value cannot be null. (Parameter 'options')
- Value cannot be null. (Parameter 'tags')
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/0dc26443c6ba5146.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/BodyPart.cs:729
/// <summary>
/// Tries to parse the given text into a new <see cref="MailKit.BodyPart"/> instance.
/// </summary>
/// <remarks>
/// <para>Parses a body part from the specified text.</para>
/// <note type="note">This syntax, while similar to IMAP's BODYSTRUCTURE syntax, is not completely
/// compatible.</note>
/// </remarks>
/// <returns><see langword="true" /> if the body part was successfully parsed; otherwise, <see langword="false" />.</returns>
/// <param name="text">The text to parse.</param>
/// <param name="part">The parsed body part.</param>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="text"/> is <see langword="null" />.
/// </exception>
public static bool TryParse (string text, out BodyPart? part)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
int index = 0;
return TryParse (text, ref index, string.Empty, out part) && index == text.Length;
}
}
}
View on GitHub (pinned to 9d3859a785)