jstedfast/MailKit · error · ArgumentNullException

Value cannot be null.

Error message

Value cannot be null.

What it means

The protected SaslMechanism(NetworkCredential credentials) constructor throws ArgumentNullException when the credentials argument is null. A SASL mechanism cannot authenticate without a NetworkCredential (user/password or OAuth token), so MailKit rejects null immediately rather than failing later during the AUTH handshake.

Solutions

  1. Provide a valid NetworkCredential instance, e.g. new NetworkCredential(userName, password) or with the OAuth2 access token.
  2. Check credentials for null before constructing the mechanism and surface a clear configuration error.
  3. Prefer the SaslMechanism(string userName, string password) constructor when you only have strings.

Example fix

// before
var mech = new SaslMechanismPlain(GetCredentials()); // GetCredentials() returned null
// after
var creds = GetCredentials();
if (creds == null) throw new InvalidOperationException("SMTP credentials are not configured");
var mech = new SaslMechanismPlain(creds);
Defensive patterns

Strategy: validation

Validate before calling

if (credentials == null)
    throw new InvalidOperationException("SASL credentials are not configured");
var mech = new SaslMechanismPlain(credentials);

Type guard

static bool HasCredentials(NetworkCredential? c) => c is { UserName: not null, Password: not null };

Try / catch

try {
    var mech = CreateMechanism(creds);
} catch (ArgumentNullException) {
    throw new InvalidOperationException("MailKit SASL credentials were null — check your auth configuration");
}

Prevention

When it happens

Trigger: Subclass constructor (e.g. SaslMechanismPlain, SaslMechanismXOAuth2) invoked with new NetworkCredential() being assigned from a null credential property, or a factory returning a mechanism built with a null credentials object.

Common situations: Config-file or environment-based credential loading that silently yields null (missing password/token key), then being passed straight into a SaslMechanism constructor.

Related errors


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

Appendix: source

Thrown at MailKit/Security/SaslMechanism.cs:149

			}

			yield break;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Security.SaslMechanism"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new SASL context.
		/// </remarks>
		/// <param name="credentials">The user's credentials.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <paramref name="credentials"/> is <see langword="null" />.
		/// </exception>
		protected SaslMechanism (NetworkCredential credentials)
		{
			if (credentials == null)
				throw new ArgumentNullException (nameof (credentials));

			Credentials = credentials;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="MailKit.Security.SaslMechanism"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new SASL context.
		/// </remarks>
		/// <param name="userName">The user name.</param>
		/// <param name="password">The password.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="userName"/> is <see langword="null" />.</para>
		/// <para>-or-</para>
		/// <para><paramref name="password"/> is <see langword="null" />.</para>
		/// </exception>
		protected SaslMechanism (string userName, string password)

View on GitHub (pinned to 9d3859a785)