{"record":{"id":"b21c63025a0d8028","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-parameter-username","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'userName')","messagePattern":"Value cannot be null\\. \\(Parameter 'userName'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/MailService.cs","lineNumber":1270,"sourceCode":"\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\n\t\t/// the SASL mechanisms that both the client and server support (not including any\n\t\t/// OAUTH mechanisms) are tried in order of greatest security to weakest security.\n\t\t/// Once a SASL authentication mechanism is found that both client and server support,","sourceCodeStart":1252,"sourceCodeEnd":1288,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/MailService.cs#L1252-L1288","documentation":"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'.","triggerScenarios":"Calling Authenticate(encoding, null, password) — userName is null.","commonSituations":"Missing SMTP/IMAP username in appsettings, environment variables, or user-secrets; credential lookups returning null when the key is absent.","solutions":["Supply the actual user name, e.g. Authenticate(Encoding.UTF8, \"user@example.com\", password)","Fix the credential source (env var, user-secrets, appsettings) so the username is populated","Guard: if (userName == null) fail with a clear 'credentials not configured' error"],"exampleFix":"// before\nclient.Authenticate(Encoding.UTF8, config[\"SMTP_USER\"], password); // SMTP_USER missing -> null\n// after\nvar user = config[\"SMTP_USER\"] ?? throw new InvalidOperationException(\"SMTP_USER not configured\");\nclient.Authenticate(Encoding.UTF8, user, password);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(userName))\n    throw new InvalidOperationException(\"Mail user name is not configured\");\nclient.Authenticate(Encoding.UTF8, userName, password);","typeGuard":"static bool HasUserName(string userName) => !string.IsNullOrEmpty(userName);","tryCatchPattern":"try { client.Authenticate(Encoding.UTF8, userName, password); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"userName\") { throw new ConfigurationException(\"Mail user name is missing (check SMTP_USER/user-secrets)\", ex); }","preventionTips":["Validate the full credential set (user + password) together at startup","Use IOptions binding with [Required] to catch missing usernames before runtime","Never pass raw config indexer results directly; they return null when keys are absent"],"tags":["mailkit","argumentnullexception","credentials","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"}