jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'password')

Error message

Value cannot be null. (Parameter 'password')

What it means

Authenticate requires a non-null password; MailKit wraps userName/password into a NetworkCredential for SASL authentication, and a null password is rejected with ArgumentNullException naming parameter 'password'.

Solutions

  1. Provide the password, preferably from a secret store, e.g. Authenticate(Encoding.UTF8, user, secret)
  2. Fix the secret configuration so the value is actually present at runtime
  3. Guard against null and surface a 'SMTP password not configured' error before calling

Example fix

// before
client.Authenticate(Encoding.UTF8, user, Environment.GetEnvironmentVariable("SMTP_PASS")); // null if unset
// after
var pass = Environment.GetEnvironmentVariable("SMTP_PASS") ?? throw new InvalidOperationException("SMTP_PASS not set");
client.Authenticate(Encoding.UTF8, user, pass);
Defensive patterns

Strategy: validation

Validate before calling

var pass = Environment.GetEnvironmentVariable("SMTP_PASS");
if (string.IsNullOrEmpty(pass))
    throw new InvalidOperationException("SMTP password not configured");
client.Authenticate(Encoding.UTF8, userName, pass);

Type guard

static bool HasPassword(string password) => !string.IsNullOrEmpty(password);

Try / catch

try { client.Authenticate(Encoding.UTF8, userName, password); }
catch (ArgumentNullException ex) when (ex.ParamName == "password") { throw new ConfigurationException("Mail password is not configured (check secret store)", ex); }

Prevention

When it happens

Trigger: Calling Authenticate(encoding, userName, null) — password is null.

Common situations: Secret not found in user-secrets/environment/Key Vault, config key typo, or secrets not injected into the container/deployment.

Related errors


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

Appendix: source

Thrown at MailKit/MailService.cs:1273

		/// <exception cref="MailKit.Security.SaslException">
		/// A SASL authentication error occurred.
		/// </exception>
		/// <exception cref="System.IO.IOException">
		/// An I/O error occurred.
		/// </exception>
		/// <exception cref="ProtocolException">
		/// A protocol error occurred.
		/// </exception>
		public void Authenticate (Encoding encoding, string userName, string password, CancellationToken cancellationToken = default)
		{
			if (encoding == null)
				throw new ArgumentNullException (nameof (encoding));

			if (userName == null)
				throw new ArgumentNullException (nameof (userName));

			if (password == null)
				throw new ArgumentNullException (nameof (password));

			var credentials = new NetworkCredential (userName, password);

			Authenticate (encoding, credentials, cancellationToken);
		}

		/// <summary>
		/// Asynchronously authenticate using the specified user name and password.
		/// </summary>
		/// <remarks>
		/// <para>Asynchronously authenticates using the supplied credentials.</para>
		/// <para>If the server supports one or more SASL authentication mechanisms, then
		/// the SASL mechanisms that both the client and server support (not including any
		/// OAUTH mechanisms) are tried in order of greatest security to weakest security.
		/// Once a SASL authentication mechanism is found that both client and server support,
		/// the credentials are used to authenticate.</para>
		/// <para>If the server does not support SASL or if no common SASL mechanisms
		/// can be found, then the default login command is used as a fallback.</para>

View on GitHub (pinned to 9d3859a785)