jstedfast/MailKit · error · ArgumentNullException

ArgumentNullException

Error message

ArgumentNullException

What it means

The FolderNotOpenException(string folderName, FolderAccess access, string message, Exception innerException) constructor throws ArgumentNullException when folderName is null. FolderNotOpenException reports which folder was not open and with what access level, so a null folderName violates its documented contract.

Solutions

  1. Pass the actual folder name (e.g. folder.FullName)
  2. Capture the folder name before the inner try block so it is in scope when throwing
  3. If no name exists, use a different exception type

Example fix

// before
throw new FolderNotOpenException (null, access, msg, inner);
// after
throw new FolderNotOpenException (folder.FullName, access, msg, inner);
Defensive patterns

Strategy: validation

Validate before calling

if (folderName == null)
    throw new InvalidOperationException ("folder name required");
throw new FolderNotOpenException (folderName, access, message, innerException);

Type guard

bool HasName (IMailFolder f) => f != null && f.FullName != null;

Try / catch

try {
    throw new FolderNotOpenException (folderName, access, message, inner);
} catch (ArgumentNullException ex) when (ex.ParamName == "folderName") {
    // rethrow with a real name
}

Prevention

When it happens

Trigger: Constructing FolderNotOpenException with a null folderName, e.g. rethrow code where the folder reference was lost.

Common situations: Wrapping IMAP operation failures on unopened folders; application-level guard helpers that build this exception generically without the name.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/74d9e06a469d3c6d. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/FolderNotOpenException.cs:90

#endif

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.FolderNotOpenException"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="FolderNotOpenException"/>.
		/// </remarks>
		/// <param name="folderName">The folder name.</param>
		/// <param name="access">The minimum folder access required by the operation.</param>
		/// <param name="message">The error message.</param>
		/// <param name="innerException">The inner exception.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="folderName"/> is <see langword="null" />.
		/// </exception>
		public FolderNotOpenException (string folderName, FolderAccess access, string message, Exception innerException) : base (message, innerException)
		{
			if (folderName == null)
				throw new ArgumentNullException (nameof (folderName));

			FolderName = folderName;
			FolderAccess = access;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.FolderNotOpenException"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="FolderNotOpenException"/>.
		/// </remarks>
		/// <param name="folderName">The folder name.</param>
		/// <param name="access">The minimum folder access required by the operation.</param>
		/// <param name="message">The error message.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="folderName"/> is <see langword="null" />.
		/// </exception>
		public FolderNotOpenException (string folderName, FolderAccess access, string message) : base (message)

View on GitHub (pinned to 9d3859a785)