{"record":{"id":"8d52291ce2cd669e","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-saslmechanism","errorCode":null,"errorMessage":"Value cannot be null.","messagePattern":"Value cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/SaslMechanism.cs","lineNumber":149,"sourceCode":"\t\t\t}\n\n\t\t\tyield break;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"MailKit.Security.SaslMechanism\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new SASL context.\n\t\t/// </remarks>\n\t\t/// <param name=\"credentials\">The user's credentials.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"credentials\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tprotected SaslMechanism (NetworkCredential credentials)\n\t\t{\n\t\t\tif (credentials == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (credentials));\n\n\t\t\tCredentials = credentials;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"MailKit.Security.SaslMechanism\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new SASL context.\n\t\t/// </remarks>\n\t\t/// <param name=\"userName\">The user name.</param>\n\t\t/// <param name=\"password\">The password.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <para><paramref name=\"userName\"/> is <see langword=\"null\" />.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para><paramref name=\"password\"/> is <see langword=\"null\" />.</para>\n\t\t/// </exception>\n\t\tprotected SaslMechanism (string userName, string password)","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/SaslMechanism.cs#L131-L167","documentation":"The protected SaslMechanism(NetworkCredential credentials) constructor throws ArgumentNullException when the credentials argument is null. A SASL mechanism cannot authenticate without a NetworkCredential (user/password or OAuth token), so MailKit rejects null immediately rather than failing later during the AUTH handshake.","triggerScenarios":"Subclass constructor (e.g. SaslMechanismPlain, SaslMechanismXOAuth2) invoked with new NetworkCredential() being assigned from a null credential property, or a factory returning a mechanism built with a null credentials object.","commonSituations":"Config-file or environment-based credential loading that silently yields null (missing password/token key), then being passed straight into a SaslMechanism constructor.","solutions":["Provide a valid NetworkCredential instance, e.g. new NetworkCredential(userName, password) or with the OAuth2 access token.","Check credentials for null before constructing the mechanism and surface a clear configuration error.","Prefer the SaslMechanism(string userName, string password) constructor when you only have strings."],"exampleFix":"// before\nvar mech = new SaslMechanismPlain(GetCredentials()); // GetCredentials() returned null\n// after\nvar creds = GetCredentials();\nif (creds == null) throw new InvalidOperationException(\"SMTP credentials are not configured\");\nvar mech = new SaslMechanismPlain(creds);","handlingStrategy":"validation","validationCode":"if (credentials == null)\n    throw new InvalidOperationException(\"SASL credentials are not configured\");\nvar mech = new SaslMechanismPlain(credentials);","typeGuard":"static bool HasCredentials(NetworkCredential? c) => c is { UserName: not null, Password: not null };","tryCatchPattern":"try {\n    var mech = CreateMechanism(creds);\n} catch (ArgumentNullException) {\n    throw new InvalidOperationException(\"MailKit SASL credentials were null — check your auth configuration\");\n}","preventionTips":["Load and validate credentials at application startup, not at connect time.","Fail fast with a descriptive config error when username/password keys are missing.","For OAuth2, verify the token retrieval succeeded before building the mechanism."],"tags":["sasl","argument-null","credentials","mailkit"],"backgroundTag":"null-argument","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}