{"record":{"id":"1eec58e569579833","repo":"jstedfast/MailKit","slug":"failed-to-connect-to-0-1-2","errorCode":null,"errorMessage":"Failed to connect to {0}:{1}: {2}","messagePattern":"Failed to connect to (.+?):(.+?): (.+?)","errorType":"exception","errorClass":"ProxyProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Proxy/HttpProxyClient.cs","lineNumber":141,"sourceCode":"\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tbuilder.Append (c);\n\n\t\t\treturn endOfHeaders;\n\t\t}\n\n\t\tinternal static void ValidateHttpResponse (string response, string host, int port)\n\t\t{\n\t\t\t// Verify that the response starts with something like \"HTTP/1.1 200 ...\"\n\t\t\tif (response.Length >= 15 && response.StartsWith (\"HTTP/1.\", StringComparison.OrdinalIgnoreCase) &&\n\t\t\t\t(response[7] == '1' || response[7] == '0') && response[8] == ' ' &&\n\t\t\t\tresponse[9] == '2' && response[10] == '0' && response[11] == '0' &&\n\t\t\t\tresponse[12] == ' ') {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthrow new ProxyProtocolException (string.Format (CultureInfo.InvariantCulture, \"Failed to connect to {0}:{1}: {2}\", host, port, response));\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Connect to the target host.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// Connects to the target host and port through the proxy server.\n\t\t/// </remarks>\n\t\t/// <returns>The connected network stream.</returns>\n\t\t/// <param name=\"host\">The host name of the target server.</param>\n\t\t/// <param name=\"port\">The target server port.</param>\n\t\t/// <param name=\"cancellationToken\">The cancellation token.</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>","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Proxy/HttpProxyClient.cs#L123-L159","documentation":"HttpProxyClient.Connect sends CONNECT host:port HTTP/1.1 to the proxy; ValidateHttpResponse requires an 'HTTP/1.0 200' or 'HTTP/1.1 200' status line. Any other status (403/407/502/503 or a non-HTTP reply) throws ProxyProtocolException with 'Failed to connect to host:port: <full response>'.","triggerScenarios":"Connecting through an HTTP proxy where the CONNECT request is denied: proxy requires authentication (407), blocks the target (403), cannot reach the target (502/503), or the endpoint is not actually an HTTP proxy (greeting is not an HTTP status line).","commonSituations":"Corporate proxy requiring credentials not supplied to the ProxyClient; proxy ACLs blocking non-standard ports like 587/995; pointing the client at a SOCKS proxy or the target server directly by mistake; proxy denying outbound traffic to the mail host.","solutions":["Read the response text in the exception message — it contains the proxy's status line and reason.","For 407, supply proxy credentials (the HttpProxyClient constructor overload that accepts ICredentials).","Confirm the proxy allows CONNECT to the target host:port (ask the network team / check ACLs).","Verify you are using HttpProxyClient with an HTTP proxy address, not a SOCKS proxy or the mail server.","Try the target connection outside the proxy to confirm the proxy, not the mail server, is the failure point."],"exampleFix":"// before\nvar proxy = new HttpProxyClient(\"proxy.corp\", 3128);\nclient.Connect(new IPEndPoint(proxyAddress...), ...); // 407 without credentials\n// after\nvar proxy = new HttpProxyClient(\"proxy.corp\", 3128, new NetworkCredential(\"user\", \"pass\"));","handlingStrategy":"try-catch","validationCode":"// pre-flight: does the proxy allow CONNECT?\n// curl -x http://proxy:3128 -I https://target:995  (expect HTTP 200)","typeGuard":null,"tryCatchPattern":"try {\n    client.Connect(target, cancellationToken);\n} catch (ProxyProtocolException ex) when (ex.Message.Contains(\"Failed to connect to\")) {\n    // inspect status in message: 407 -> add credentials, 403/502 -> proxy ACL/target issue\n}","preventionTips":["Configure proxy credentials when the corporate proxy requires authentication","Confirm with network ops that CONNECT to mail ports is permitted","Ensure you picked HttpProxyClient for HTTP proxies and Socks*Client for SOCKS"],"tags":["proxy","http","connection","mailkit","network"],"backgroundTag":"http-error-response","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"}