jstedfast/MailKit · error · ArgumentNullException

Value cannot be null. (Parameter 'userName')

Error message

Value cannot be null. (Parameter 'userName')

What it means

Authenticate requires a non-null user name; MailKit builds a NetworkCredential from userName and password, and a null userName would produce invalid SASL credentials. It throws ArgumentNullException with parameter name 'userName'.

Solutions

  1. Supply the actual user name, e.g. Authenticate(Encoding.UTF8, "user@example.com", password)
  2. Fix the credential source (env var, user-secrets, appsettings) so the username is populated
  3. Guard: if (userName == null) fail with a clear 'credentials not configured' error

Example fix

// before
client.Authenticate(Encoding.UTF8, config["SMTP_USER"], password); // SMTP_USER missing -> null
// after
var user = config["SMTP_USER"] ?? throw new InvalidOperationException("SMTP_USER not configured");
client.Authenticate(Encoding.UTF8, user, password);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(userName))
    throw new InvalidOperationException("Mail user name is not configured");
client.Authenticate(Encoding.UTF8, userName, password);

Type guard

static bool HasUserName(string userName) => !string.IsNullOrEmpty(userName);

Try / catch

try { client.Authenticate(Encoding.UTF8, userName, password); }
catch (ArgumentNullException ex) when (ex.ParamName == "userName") { throw new ConfigurationException("Mail user name is missing (check SMTP_USER/user-secrets)", ex); }

Prevention

When it happens

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

Common situations: Missing SMTP/IMAP username in appsettings, environment variables, or user-secrets; credential lookups returning null when the key is absent.

Related errors


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

Appendix: source

Thrown at MailKit/MailService.cs:1270

		/// <exception cref="MailKit.Security.AuthenticationException">
		/// Authentication using the supplied credentials has failed.
		/// </exception>
		/// <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,

View on GitHub (pinned to 9d3859a785)