jstedfast/MailKit · error · ArgumentNullException
Value cannot be null. (Parameter 'encoding')
Error message
Value cannot be null. (Parameter 'encoding')
What it means
Authenticate(Encoding, string, string) uses the given Encoding to encode the user name and password during SASL authentication. A null Encoding is invalid, so MailKit throws ArgumentNullException naming the 'encoding' parameter.
Solutions
- Pass a concrete encoding such as Encoding.UTF8
- If the encoding comes from config, default to Encoding.UTF8 when null
- Validate the config value with Encoding.GetEncoding(name) early and fail with a clear message
Example fix
// before client.Authenticate(encoding, "user", "pass"); // encoding is null // after client.Authenticate(encoding ?? Encoding.UTF8, "user", "pass");
Defensive patterns
Strategy: validation
Validate before calling
var enc = encoding ?? Encoding.UTF8; // or resolve from config name
if (enc == null) throw new InvalidOperationException("Mail encoding not configured");
client.Authenticate(enc, userName, password); Type guard
static bool HasEncoding(Encoding encoding) => encoding != null;
Try / catch
try { client.Authenticate(encoding, userName, password); }
catch (ArgumentNullException ex) when (ex.ParamName == "encoding") { throw new ConfigurationException("Mail authentication encoding must not be null", ex); } Prevention
- Default to Encoding.UTF8 when encoding is optional or config-driven
- Resolve encoding names with Encoding.GetEncoding and fail at startup for invalid names
- Keep encoding out of nullable paths when possible (initialize once in a options class)
When it happens
Trigger: Calling Authenticate(null, "user", "pass"), typically when the encoding comes from a nullable variable or a config lookup that returned null.
Common situations: Encoding.GetEncoding from an invalid/unregistered code page name wrapped in try/catch returning null, or optional DI/config values left unset.
Related errors
- Value cannot be null. (Parameter 'userName')
- Value cannot be null. (Parameter 'password')
- message
- The ImapClient is already authenticated.
- The LOGIN command is disabled.
AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15).
Data as JSON: /api/errors/ac6229026e40b943.
Report an issue: GitHub.
Appendix: source
Thrown at MailKit/MailService.cs:1267
/// <exception cref="System.OperationCanceledException">
/// The operation was canceled via the cancellation token.
/// </exception>
/// <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, thenView on GitHub (pinned to 9d3859a785)