{"record":{"id":"48f2fa05ba8a1cfc","repo":"jstedfast/MailKit","slug":"the-length-of-the-host-name-must-be-between-0-and-256","errorCode":null,"errorMessage":"The length of the host name must be between 0 and 256 characters.","messagePattern":"The length of the host name must be between 0 and 256 characters\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Proxy/ProxyClient.cs","lineNumber":96,"sourceCode":"\t\t/// <param name=\"port\">The proxy server port.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">\n\t\t/// <paramref name=\"host\"/> is <see langword=\"null\" />.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentOutOfRangeException\">\n\t\t/// <paramref name=\"port\"/> is not between <c>0</c> and <c>65535</c>.\n\t\t/// </exception>\n\t\t/// <exception cref=\"System.ArgumentException\">\n\t\t/// <para>The <paramref name=\"host\"/> is a zero-length string.</para>\n\t\t/// <para>-or-</para>\n\t\t/// <para>The length of <paramref name=\"host\"/> is greater than 255 characters.</para>\n\t\t/// </exception>\n\t\tprotected ProxyClient (string host, int port)\n\t\t{\n\t\t\tif (host == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (host));\n\n\t\t\tif (host.Length == 0 || host.Length > 255)\n\t\t\t\tthrow new ArgumentException (\"The length of the host name must be between 0 and 256 characters.\", nameof (host));\n\n\t\t\tif (port < 0 || port > 65535)\n\t\t\t\tthrow new ArgumentOutOfRangeException (nameof (port));\n\n\t\t\tProxyHost = host;\n\t\t\tProxyPort = port == 0 ? 1080 : port;\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.Net.Proxy.ProxyClient\"/> class.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Initializes a new instance of the <see cref=\"T:MailKit.Net.Proxy.ProxyClient\"/> class.\n\t\t/// </remarks>\n\t\t/// <param name=\"host\">The host name of the proxy server.</param>\n\t\t/// <param name=\"port\">The proxy server port.</param>\n\t\t/// <param name=\"credentials\">The credentials to use to authenticate with the proxy server.</param>\n\t\t/// <exception cref=\"System.ArgumentNullException\">","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Proxy/ProxyClient.cs#L78-L114","documentation":"The protected ProxyClient constructor validates the proxy host name: it must be non-null, non-empty, and at most 255 characters, otherwise ArgumentException('The length of the host name must be between 0 and 256 characters.') is thrown (ArgumentNullException for null). This runs when constructing any proxy client (Http, Socks4/5).","triggerScenarios":"Constructing Socks4Client/Socks5Client/HttpProxyClient with host = \"\" or a string longer than 255 chars — usually an empty config/environment variable silently substituted for the proxy host.","commonSituations":"Missing PROXY_HOST env var read as string.Empty; copy-paste or trimming bug producing an empty host; config file where the proxy section is blank; unlikely >255-char host from malformed data.","solutions":["Validate the proxy host from configuration before constructing: it must be non-empty and <= 255 chars.","Check where the value is read (env var / appSettings) for empty or missing values and fail fast with a clear config error.","If no proxy is needed, do not construct a ProxyClient at all instead of passing an empty host.","Trim whitespace and re-validate; a whitespace-only string still effectively fails the connection."],"exampleFix":"// before\nvar proxy = new Socks5Client(config.ProxyHost, config.ProxyPort); // ProxyHost == \"\"\n// after\nif (string.IsNullOrWhiteSpace(config.ProxyHost) || config.ProxyHost.Length > 255)\n    throw new InvalidOperationException(\"PROXY_HOST is missing or invalid\");\nvar proxy = new Socks5Client(config.ProxyHost.Trim(), config.ProxyPort);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(host) || host.Length > 255)\n    throw new InvalidOperationException(\"Proxy host missing or too long in configuration\");","typeGuard":"bool IsValidProxyHost(string? h) => !string.IsNullOrWhiteSpace(h) && h.Length <= 255;","tryCatchPattern":"try {\n    var proxy = new HttpProxyClient(host, port);\n} catch (ArgumentException ex) when (ex.ParamName == \"host\") {\n    // fall back to direct connection or surface a config error\n}","preventionTips":["Validate proxy settings at application startup, before any connection attempt","Never pass empty strings for 'no proxy' — use a null ProxyClient instead","Trim and null-check values read from env vars/config files"],"tags":["proxy","argument-validation","configuration","mailkit"],"backgroundTag":"invalid-argument-value","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"}