{"record":{"id":"ac6229026e40b943","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-encoding","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'encoding')","messagePattern":"Value cannot be null\\. \\(Parameter 'encoding'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/MailService.cs","lineNumber":1267,"sourceCode":"\t\t/// <exception cref=\"System.OperationCanceledException\">\n\t\t/// The operation was canceled via the cancellation token.\n\t\t/// </exception>\n\t\t/// <exception cref=\"MailKit.Security.AuthenticationException\">\n\t\t/// Authentication using the supplied credentials has failed.\n\t\t/// </exception>\n\t\t/// <exception cref=\"MailKit.Security.SaslException\">\n\t\t/// A SASL authentication error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.IO.IOException\">\n\t\t/// An I/O error occurred.\n\t\t/// </exception>\n\t\t/// <exception cref=\"ProtocolException\">\n\t\t/// A protocol error occurred.\n\t\t/// </exception>\n\t\tpublic void Authenticate (Encoding encoding, string userName, string password, CancellationToken cancellationToken = default)\n\t\t{\n\t\t\tif (encoding == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (encoding));\n\n\t\t\tif (userName == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (userName));\n\n\t\t\tif (password == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (password));\n\n\t\t\tvar credentials = new NetworkCredential (userName, password);\n\n\t\t\tAuthenticate (encoding, credentials, cancellationToken);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Asynchronously authenticate using the specified user name and password.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Asynchronously authenticates using the supplied credentials.</para>\n\t\t/// <para>If the server supports one or more SASL authentication mechanisms, then","sourceCodeStart":1249,"sourceCodeEnd":1285,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailService.cs#L1249-L1285","documentation":"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.","triggerScenarios":"Calling Authenticate(null, \"user\", \"pass\"), typically when the encoding comes from a nullable variable or a config lookup that returned null.","commonSituations":"Encoding.GetEncoding from an invalid/unregistered code page name wrapped in try/catch returning null, or optional DI/config values left unset.","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"],"exampleFix":"// before\nclient.Authenticate(encoding, \"user\", \"pass\"); // encoding is null\n// after\nclient.Authenticate(encoding ?? Encoding.UTF8, \"user\", \"pass\");","handlingStrategy":"validation","validationCode":"var enc = encoding ?? Encoding.UTF8; // or resolve from config name\nif (enc == null) throw new InvalidOperationException(\"Mail encoding not configured\");\nclient.Authenticate(enc, userName, password);","typeGuard":"static bool HasEncoding(Encoding encoding) => encoding != null;","tryCatchPattern":"try { client.Authenticate(encoding, userName, password); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"encoding\") { throw new ConfigurationException(\"Mail authentication encoding must not be null\", ex); }","preventionTips":["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)"],"tags":["mailkit","argumentnullexception","encoding","authentication"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}