{"id":1587,"date":"2010-11-15T15:26:30","date_gmt":"2010-11-15T13:26:30","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=1587"},"modified":"2018-04-03T13:17:59","modified_gmt":"2018-04-03T11:17:59","slug":"connection-attempt-failed","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/connection-attempt-failed","title":{"rendered":"A connection attempt failed"},"content":{"rendered":"<p>If you are getting following or similar exception:<br \/>\n<strong>&#8220;A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond&#8221;<\/strong> or <strong>&#8220;No connection could be made because the target machine actively refused it.&#8221;<\/strong> most likely you provided <strong>incorrect server, port, and SSL usage<\/strong> configuration to <em>Connect<\/em>, or <em>ConnectSSL <\/em>methods.<\/p>\n<p>Default ports for email protocols are:<\/p>\n<table>\n<tr>\n<th><\/th>\n<th>Plain text<\/th>\n<th>SSL\/TLS<\/th>\n<\/tr>\n<tr>\n<td style=\"padding:1em;\"><strong>IMAP<\/strong><\/td>\n<td style=\"padding:1em;\">143  <\/td>\n<td style=\"padding:1em;\">993<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:1em;\"><strong>POP3<\/strong><\/td>\n<td style=\"padding:1em;\">110 <\/td>\n<td style=\"padding:1em;\">995<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:1em;\"><strong>SMTP<\/strong><\/td>\n<td style=\"padding:1em;\">587 or 25<\/td>\n<td style=\"padding:1em;\">465<\/td>\n<\/tr>\n<\/table>\n<p>Most email providers (like Gmail, Hotmail, Yahoo and others) use default ports. <em>Connect<\/em>() and <em>ConnectSSL<\/em>() and fluent interface use those ports by default.<\/p>\n<p><strong>Please ask your email server administrator for server, port, and SSL\/TLS usage<\/strong>, if those are not standard there is no way you can guess them.<\/p>\n<p>Please also make sure that:<\/p>\n<ul>\n<li>your application has <strong>enough security permissions<\/strong> to establish connection to the server.<\/li>\n<li>you are using correct client class with protocol you plan to use (<em>Pop3 <\/em>class with POP3 protocol, <em>Imap <\/em>class with IMAP, and <em>Smtp <\/em>class with SMTP protocol)<\/li>\n<li>you use correct port numbers, and rely on parameter-less <em>Connect()<\/em> and <em>ConnectSSL()<\/em> methods (they use default ports) whenever possible<\/li>\n<li>you are using <em>ConnectSSL <\/em>when you require SSL\/TLS, and <em>Connect <\/em>when you don&#8217;t<\/li>\n<li><strong>firewall and antivirus software are disabled<\/strong> or configured correctly (this includes Windows Defender)<\/li>\n<li>the protocol you are trying to use is enabled on the server. Exchange or Gmail can have some protocols disabled by default<\/li>\n<\/ul>\n<p>Following articles can help you with that:<\/p>\n<ul>\n<li><a href=\"\/blog\/enable-pop3-in-gmail\">How to enable POP3 in Gmail<\/a><\/li>\n<li><a href=\"\/blog\/enable-imap-in-gmail\">How to enable IMAP in  Gmail<\/a><\/li>\n<li><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb124934.aspx\">How to enable POP3 in Exchange<\/a><\/li>\n<li><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb124489.aspx\">How to enable IMAP in Exchange<\/a><\/li>\n<\/ul>\n<h2>Connecting using default ports<\/h2>\n<h3>Plain text mode<\/h3>\n<p>Establishing connection using default port is simple, you just need to use <em>Connect<\/em> method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nclient.Connect(&quot;mail.example.com&quot;);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nclient.Connect(&quot;mail.example.com&quot;)\r\n<\/pre>\n<h3>Implicit SSL\/TLS mode<\/h3>\n<p>If SSL encryption is required, use <em>ConnectSSL<\/em> method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nclient.ConnectSSL(&quot;mail.example.com&quot;);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nclient.ConnectSSL(&quot;mail.example.com&quot;)\r\n<\/pre>\n<h3>Explicit SSL mode (aka TLS)<\/h3>\n<p>If your server requires explicit SSL\/TLS (sometimes called TLS), you need to connect using <em>Connect<\/em> method and then use <em>StartTLS<\/em> method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nclient.Connect(&quot;mail.example.com&quot;);\r\nclient.StartTLS();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nclient.ConnectSSL(&quot;mail.example.com&quot;)\r\nclient.StartTLS()\r\n<\/pre>\n<h2>Connecting using non-standard ports<\/h2>\n<p>If you need to specify different port just <strong>use overloaded<\/strong> version of <em>Connect<\/em> or <em>ConnectSSL<\/em> method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nclient.Connect(&quot;mail.example.com&quot;, 999);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nclient.Connect(&quot;mail.example.com&quot;, 999)\r\n<\/pre>\n<p>If SSL\/TLS encryption is required:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C#\r\n\r\nclient.ConnectSSL(&quot;mail.example.com&quot;, 999);\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET\r\n\r\nclient.ConnectSSL(&quot;mail.example.com&quot;, 999)\r\n<\/pre>\n<p>If you need to specify different port using fluent interface use OnPort() method:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nMail.Text(@&quot;Hello&quot;)\r\n  .To(&quot;to@mail.com&quot;)\r\n  .From(&quot;from@mail.com&quot;)\r\n  .Subject(&quot;Subject&quot;)\r\n  .UsingNewSmtp()\r\n  .Server(_server)\r\n  .WithSSL()\r\n  .OnPort(443)\r\n  .WithCredentials(&quot;user&quot;, &quot;password&quot;)\r\n  .Send();\r\n<\/pre>\n<h2>SSL\/TLS<\/h2>\n<p>Please look at following articles for details:<\/p>\n<ul>\n<li><a href=\"\/blog\/use-ssl-with-imap\">Use SSL with IMAP<\/a><\/li>\n<li><a href=\"\/blog\/use-ssl-with-pop3\">Use SSL with POP3<\/a><\/li>\n<li><a href=\"\/blog\/use-ssl-with-smtp\">Use SSL with SMTP<\/a><\/li>\n<\/ul>\n<p>Some servers require explicit SSL\/TLS (sometimes called TLS). Usually this triggers <a href=\"\/blog\/the-handshake-failed-due-to-an-unexpected-packet-format\">&#8220;The handshake failed due to an unexpected packet format&#8221;<\/a> exception, when you try to connect without SSL\/TLS.<\/p>\n<p>If your server uses self-signed certificates you may be getting <a href=\"\/blog\/the-remote-certificate-is-invalid-according-to-the-validation-procedure\">&#8220;The remote certificate is invalid according to the validation procedure&#8221;<\/a> error.<\/p>\n<p>Please read <a href=\"\/blog\/ssl-vs-tls-vs-starttls-stls\">SSL vs TLS vs STARTTLS<\/a>, if you are confused about those.<\/p>\n<p>Again when in doubt<strong> please ask your email server administrator for server, port, and SSL\/TLS usage<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are getting following or similar exception: &#8220;A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond&#8221; or &#8220;No connection could be made because the target machine actively refused it.&#8221; most likely you provided incorrect server, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[15,21,25,28,42,50,53,83,57],"class_list":["post-1587","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-c","tag-exchange","tag-gmail","tag-imap","tag-pop3","tag-smtp","tag-ssl","tag-tls","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1587"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=1587"}],"version-history":[{"count":20,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1587\/revisions"}],"predecessor-version":[{"id":5433,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/1587\/revisions\/5433"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=1587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=1587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=1587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}