jstedfast/MailKit · error · AuthenticationException

User name too long.

Error message

User name too long.

What it means

The SOCKS5 username/password (RFC 1929) sub-negotiation limits each field to a single length byte, i.e. 255 bytes. GetAuthenticateCommand throws AuthenticationException when the UTF-8 encoded UserName exceeds 255 bytes, since it cannot be encoded on the wire.

Solutions

  1. Shorten the username to 255 UTF-8 bytes or less (trim domain prefix if the proxy allows bare username)
  2. Use an authentication method without the 255-byte limit, or pre-authorize by IP so no username is needed
  3. Count bytes, not characters, when validating credentials before configuring the client

Example fix

// before
var name = "cn=svc,ou=proxy,dc=example,dc=com;" + longToken; // >255 bytes
credentials.UserName = name;
// after
var name = "svc-proxy"; // short, pre-registered on the proxy
credentials.UserName = name;
Defensive patterns

Strategy: validation

Validate before calling

if (Encoding.UTF8.GetByteCount(user) > 255)
    throw new ArgumentException("SOCKS5 username must be <= 255 UTF-8 bytes.", nameof(user));

Prevention

When it happens

Trigger: Socks5Client.Connect/ConnectAsync with ProxyCredentials whose UserName encodes to more than 255 UTF-8 bytes.

Common situations: Users pasting credentials containing a whole token/URL or a JWT as the username; enterprise credential strings with long domain prefixes; multi-byte characters (e.g. CJK) pushing a nominally short username over 255 bytes.

Related errors


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

Appendix: source

Thrown at MailKit/Net/Proxy/Socks5Client.cs:242

			// |  1  |   1    |
			// +-----+--------+
			int nread, n = 0;
			do {
				if ((nread = await ReceiveAsync (socket, buffer, 0 + n, 2 - n, cancellationToken).ConfigureAwait (false)) > 0)
					n += nread;
			} while (n < 2);

			VerifySocksVersion (buffer[0]);

			return (Socks5AuthMethod) buffer[1];
		}

		byte[] GetAuthenticateCommand ()
		{
			var user = Encoding.UTF8.GetBytes (ProxyCredentials!.UserName);

			if (user.Length > 255)
				throw new AuthenticationException ("User name too long.");

			var passwd = Encoding.UTF8.GetBytes (ProxyCredentials.Password);

			if (passwd.Length > 255) {
				Array.Clear (passwd, 0, passwd.Length);
				throw new AuthenticationException ("Password too long.");
			}

			var buffer = new byte[user.Length + passwd.Length + 3];
			int n = 0;

			buffer[n++] = 1;
			buffer[n++] = (byte) user.Length;
			Buffer.BlockCopy (user, 0, buffer, n, user.Length);
			n += user.Length;
			buffer[n++] = (byte) passwd.Length;
			Buffer.BlockCopy (passwd, 0, buffer, n, passwd.Length);

View on GitHub (pinned to 9d3859a785)