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:
Plain text | SSL/TLS | |
---|---|---|
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/TLS 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/TLS, 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:
- How to enable POP3 in Gmail
- How to enable IMAP in Gmail
- How to enable POP3 in Exchange
- How to enable IMAP in Exchange
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/TLS 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/TLS 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/TLS.
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/TLS usage.