{"record":{"id":"e2b0f5a0e5c0c9d8","repo":"jstedfast/MailKit","slug":"value-cannot-be-null-saslexception","errorCode":null,"errorMessage":"Value cannot be null.","messagePattern":"Value cannot be null\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"MailKit/Security/SaslException.cs","lineNumber":108,"sourceCode":"\t\t}\n#endif\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"MailKit.Security.SaslException\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Creates a new <see cref=\"SaslException\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"mechanism\">The SASL mechanism.</param>\n\t\t/// <param name=\"code\">The error code.</param>\n\t\t/// <param name=\"message\">The error message.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"mechanism\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\tpublic SaslException (string mechanism, SaslErrorCode code, string message) : base (message)\n\t\t{\n\t\t\tif (mechanism == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (mechanism));\n\n\t\t\tMechanism = mechanism;\n\t\t\tErrorCode = code;\n\t\t}\n\n#if SERIALIZABLE\n\t\t/// <summary>\n\t\t/// When overridden in a derived class, sets the <see cref=\"System.Runtime.Serialization.SerializationInfo\"/>\n\t\t/// with information about the exception.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Serializes the state of the <see cref=\"SaslException\"/>.\n\t\t/// </remarks>\n\t\t/// <param name=\"info\">The serialization info.</param>\n\t\t/// <param name=\"context\">The streaming context.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"info\"/> is <see langword=\"null\" />.\n\t\t/// </exception>","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Security/SaslException.cs#L90-L126","documentation":"The SaslException(string mechanism, SaslErrorCode code, string message) constructor throws ArgumentNullException when the mechanism name is null. The mechanism string identifies the SASL mechanism the error relates to, so the exception requires it to be non-null even when a custom message is supplied.","triggerScenarios":"Throwing new SaslException(null, code, \"...\") from application code, or a SASL handling routine that lost the mechanism name (e.g. an unparsed server challenge) and passes null as the first argument.","commonSituations":"Wrapping a failed AUTH step where the mechanism variable was never initialized, or constructing the exception from deserialized/partial data where the mechanism field is missing.","solutions":["Pass the actual mechanism name string (e.g. \"PLAIN\", \"XOAUTH2\") as the first constructor argument.","If the mechanism is genuinely unknown, pass a descriptive placeholder such as string.Empty or the raw server-advertised token, never null.","Use the simpler SaslException(string message) constructor when no mechanism identity is available."],"exampleFix":"// before\nthrow new SaslException(null, SaslErrorCode.Failure, \"auth failed\");\n// after\nthrow new SaslException(mechanism ?? \"unknown\", SaslErrorCode.Failure, \"auth failed\");","handlingStrategy":"validation","validationCode":"if (mechanism == null)\n    throw new InvalidOperationException(\"Cannot raise SaslException without a mechanism name\");\nthrow new SaslException(mechanism, code, message);","typeGuard":"bool IsValidMechanism(string? m) => !string.IsNullOrEmpty(m);","tryCatchPattern":"try {\n    client.Authenticate(mechanism, creds);\n} catch (SaslException ex) {\n    logger.LogWarning(\"SASL failure on {Mechanism}: {Error}\", ex.Mechanism, ex.Message);\n    throw;\n}","preventionTips":["Never pass null for the mechanism name; use string.Empty when unknown.","Track the negotiated mechanism in a non-null field once the server advertises it.","Prefer the SaslException(string message) overload when no mechanism context exists."],"tags":["sasl","argument-null","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-16T04:17:20.429Z"}