A connection attempt failed

If you are getting following or similar exception:
“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” or “No connection could be made because the target machine actively refused it.” most likely you provided incorrect server, port, and SSL usage configuration to Connect, or ConnectSSL methods.

Default ports for email protocols are:

Protocol Plain text SSL
IMAP 143 993
POP3 110 995
SMTP 587 or 25 465

Most email providers (like Gmail, Hotmail, Yahoo and others) use default ports. Connect() and ConnectSSL() and fluent interface use those ports by default.

Please ask your email server administrator for server, port, and SSL usage, if those are not standard there is no way you can guess them.

Please also make sure that:

  • your application has enough security permissions to establish connection to the server.
  • you are using correct client class with protocol you plan to use (Pop3 class with POP3 protocol, Imap class with IMAP, and Smtp class with SMTP protocol)
  • you use correct port numbers, and rely on parameter-less Connect() and ConnectSSL() methods (they use default ports) whenever possible
  • you are using ConnectSSL when you require SSL, and Connect when you don’t
  • firewall and antivirus software are disabled or configured correctly (this includes Windows Defender)
  • the protocol you are trying to use is enabled on the server. Exchange or Gmail can have some protocols disabled by default

Following articles can help you with that:

Connecting using default ports

Plain text mode

Establishing connection using default port is simple, you just need to use Connect method:

// C#

client.Connect("mail.example.com");
' VB.NET

client.Connect("mail.example.com")

Implicit SSL mode

If SSL encryption is required, use ConnectSSL method:

// C#

client.ConnectSSL("mail.example.com");
' VB.NET

client.ConnectSSL("mail.example.com")

Explicit SSL mode (aka TLS)

If your server requires explicit SSL/TLS (sometimes called TLS), you need to connect using Connect method and then use StartTLS method:

// C#

client.Connect("mail.example.com");
client.StartTLS();
' VB.NET

client.ConnectSSL("mail.example.com")
client.StartTLS()

Connecting using non-standard ports

If you need to specify different port just use overloaded version of Connect or ConnectSSL method:

// C#

client.Connect("mail.example.com", 999);
' VB.NET

client.Connect("mail.example.com", 999)

If SSL encryption is required:

// C#

client.ConnectSSL("mail.example.com", 999);
' VB.NET

client.ConnectSSL("mail.example.com", 999)

If you need to specify different port using fluent interface use OnPort() method:

// C# version

Mail.Text(@"Hello")
  .To("to@mail.com")
  .From("from@mail.com")
  .Subject("Subject")
  .UsingNewSmtp()
  .Server(_server)
  .WithSSL()
  .OnPort(443)
  .WithCredentials("user", "password")
  .Send();

SSL/TLS

Please look at following articles for details:

Some servers require explicit SSL/TLS (sometimes called TLS). Usually this triggers “The handshake failed due to an unexpected packet format” exception, when you try to connect without SSL.

If your server uses self-signed certificates you may be getting “The remote certificate is invalid according to the validation procedure” error.

Please read SSL vs TLS vs STARTTLS, if you are confused about those.

Again when in doubt please ask your email server administrator for server, port, and SSL usage.

Tags: , , , , , , , ,

8 Responses to “A connection attempt failed”

  1. Robert Nguyen Says:

    Do you have document on how to setup SMTP server or POP3. We bought your software and try to setup local development box to test smtp or pop3 email.

    Thanks

    Robert Ng

  2. Limilabs support Says:

    @Robert
    I don’t have such document, as you may be using hundred different smtp/pop3/imap servers on Windows or Linux. If you have a server already installed and running please refer to its manual on how to enable needed protocols, ssl and which ports server is using.

    For a windows development box I can recommend hMailServer.
    It’s easy to use and supports Smtp, Pop3, Imap, SSL and DKIM.

  3. Carlos Vaca Says:

    I have this problem: 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…

    I started tried with SSL and used the port that use my mail provider “GoDaddy”. I had connection problems, so i decided to use the connection without the SSL. I have the same way connection of the samples and i still have problems, i know it because a have a log of the issues. The ports are open in the server.

    What else can i do?

  4. Limilabs support Says:

    @Carlos

    Does your application have enough permissions to create connection to the server (SocketPermission)?

  5. Manoj Says:

    Hi,
    I am writing an app where i need to send email to some clients. Can i do it using your component but without using SMTP Server.

    Thx
    Manoj

  6. Limilabs support Says:

    @Manoj,

    No, you can’t send email without the services of a SMTP server.
    You can however send email using recipient’s DNS server.

  7. Daniel Says:

    how about error handling in your component and detailed diagnostics?

    For Windows 8 version when I enter wrong server address for Connect(SSL) async methods they just break without exception, without status, no info, nothing… they just go back to UI thread.

  8. Limilabs support Says:

    @Daniel,

    I think you are wrong: ConnectSSL method is awaited – thus execution “returns” to UI thread.

    Use try..catch blocks when necessary:

    using (Imap client = new Imap ())
    {
        try
        {
            await client.ConnectSSL("imap.incorrect.com");
        }
        catch(Exception ex)
        {
            Log(ex.Message);
            return;
        }
        //...
        client.Close();
    }
    

    As connection is created asynchronously and .NET gives up after 20 seconds, you’ll get “A connection attempt failed…” exception after this time.

Questions?

Consider using our Q&A forum for asking any questions.