jstedfast/MailKit · error · ArgumentNullException

Value cannot be null.

Error message

Value cannot be null.

What it means

The SaslException(string mechanism, SaslErrorCode code, string message) constructor throws ArgumentNullException when the mechanism name is null. The mechanism string identifies the SASL mechanism the error relates to, so the exception requires it to be non-null even when a custom message is supplied.

Solutions

  1. Pass the actual mechanism name string (e.g. "PLAIN", "XOAUTH2") as the first constructor argument.
  2. If the mechanism is genuinely unknown, pass a descriptive placeholder such as string.Empty or the raw server-advertised token, never null.
  3. Use the simpler SaslException(string message) constructor when no mechanism identity is available.

Example fix

// before
throw new SaslException(null, SaslErrorCode.Failure, "auth failed");
// after
throw new SaslException(mechanism ?? "unknown", SaslErrorCode.Failure, "auth failed");
Defensive patterns

Strategy: validation

Validate before calling

if (mechanism == null)
    throw new InvalidOperationException("Cannot raise SaslException without a mechanism name");
throw new SaslException(mechanism, code, message);

Type guard

bool IsValidMechanism(string? m) => !string.IsNullOrEmpty(m);

Try / catch

try {
    client.Authenticate(mechanism, creds);
} catch (SaslException ex) {
    logger.LogWarning("SASL failure on {Mechanism}: {Error}", ex.Mechanism, ex.Message);
    throw;
}

Prevention

When it happens

Trigger: Throwing new SaslException(null, code, "...") from application code, or a SASL handling routine that lost the mechanism name (e.g. an unparsed server challenge) and passes null as the first argument.

Common situations: Wrapping a failed AUTH step where the mechanism variable was never initialized, or constructing the exception from deserialized/partial data where the mechanism field is missing.

Related errors


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

Appendix: source

Thrown at MailKit/Security/SaslException.cs:108

		}
#endif

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Security.SaslException"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="SaslException"/>.
		/// </remarks>
		/// <param name="mechanism">The SASL mechanism.</param>
		/// <param name="code">The error code.</param>
		/// <param name="message">The error message.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="mechanism"/> is <see langword="null" />.
		/// </exception>
		public SaslException (string mechanism, SaslErrorCode code, string message) : base (message)
		{
			if (mechanism == null)
				throw new ArgumentNullException (nameof (mechanism));

			Mechanism = mechanism;
			ErrorCode = code;
		}

#if SERIALIZABLE
		/// <summary>
		/// When overridden in a derived class, sets the <see cref="System.Runtime.Serialization.SerializationInfo"/>
		/// with information about the exception.
		/// </summary>
		/// <remarks>
		/// Serializes the state of the <see cref="SaslException"/>.
		/// </remarks>
		/// <param name="info">The serialization info.</param>
		/// <param name="context">The streaming context.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="info"/> is <see langword="null" />.
		/// </exception>

View on GitHub (pinned to 9d3859a785)