jstedfast/MailKit · error · ArgumentException

One or more characters in the string are prohibited.

Error message

One or more characters in the string are prohibited.

What it means

During SASLprep (RFC 4013) normalization, SaslMechanism checks each character against the StringPrep prohibited tables (e.g. non-ASCII space, prohibited output, surrogates, tags). If any character is prohibited, it throws ArgumentException because the string cannot be used as a SASL identity.

Solutions

  1. Sanitize the input: replace prohibited/non-standard whitespace with a regular space or remove it before constructing the mechanism.
  2. Normalize the string with s.Normalize(NormalizationForm.FormKC) and re-validate before passing it in.
  3. Catch ArgumentException and prompt the user to correct the offending credential value.

Example fix

// before
var mech = new SaslMechanismPlain(userName); // userName has U+00A0
// after
var clean = userName.Normalize(NormalizationForm.FormKC).Replace('\u00A0', ' ');
var mech = new SaslMechanismPlain(clean);
Defensive patterns

Strategy: validation

Validate before calling

static string SanitizeSaslString(string s) {
    var clean = new string(s.Where(c => !char.IsControl(c)).ToArray());
    clean = clean.Normalize(NormalizationForm.FormKC).Replace('\u00A0', ' ');
    return clean;
}
var mech = new SaslMechanismPlain(SanitizeSaslString(userName));

Try / catch

try {
    var mech = new SaslMechanismPlain(userName);
} catch (ArgumentException) {
    // username contains StringPrep-prohibited characters
}

Prevention

When it happens

Trigger: Passing a username/authorization id containing prohibited characters such as non-ASCII spaces (U+00A0), surrogates, or other StringPrep-table characters to a SaslMechanism constructor or credential property.

Common situations: Usernames copied from word processors containing non-breaking spaces, IDs containing emoji/surrogate pairs, credentials pasted from PDFs with exotic whitespace.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of jstedfast/MailKit@9d3859a785 (2026-09-15). Data as JSON: /api/errors/8d92fb67c0bfbe50. Report an issue: GitHub.

Appendix: source

Thrown at MailKit/Security/SaslMechanism.cs:744

			if (s == null)
				throw new ArgumentNullException (nameof (s));

			if (s.Length == 0)
				return s;

			var builder = new StringBuilder (s.Length);
			for (int i = 0; i < s.Length; i++) {
				if (IsNonAsciiSpace (s[i])) {
					// non-ASCII space characters [StringPrep, C.1.2] that can be
					// mapped to SPACE (U+0020).
					builder.Append (' ');
				} else if (IsCommonlyMappedToNothing (s[i])) {
					// the "commonly mapped to nothing" characters [StringPrep, B.1]
					// that can be mapped to nothing.
				} else if (char.IsControl (s[i])) {
					throw new ArgumentException ("Control characters are prohibited.", nameof (s));
				} else if (IsProhibited (s, i)) {
					throw new ArgumentException ("One or more characters in the string are prohibited.", nameof (s));
				} else {
					builder.Append (s[i]);
				}
			}

			return builder.ToString ().Normalize (NormalizationForm.FormKC);
		}

		internal static string GenerateEntropy (int n)
		{
			var entropy = new byte[n];

			using (var rng = RandomNumberGenerator.Create ())
				rng.GetBytes (entropy);

			return Convert.ToBase64String (entropy);
		}
	}

View on GitHub (pinned to 9d3859a785)