A connection attempt failed
Monday, November 15th, 2010If 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.
Most email providers (like Gmail, Hotmail, Yahoo and others) use default ports. Connect() and ConnectSSL() and fluent interface use those ports when not specified otherwise.
Default ports for email protocols are:
| Protocol | Without SSL | With SSL |
|---|---|---|
| IMAP | 143 | 993 |
| POP3 | 110 | 995 |
| SMTP | 25 | 465 |
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:
- you are using correct client class with protocol and port you plan to use (Pop3 class with POP3 protocol, Imap class with IMAP, and Smtp class with SMTP protocol)
- you are using ConnectSSL when you require SSL, and Connect when you don’t
- firewall and antivirus software are disabled or configured correctly
- 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:
Establishing connection using default port is simple:
// C#
client.Connect("mail.example.com");
' VB.NET
client.Connect("mail.example.com")
or if SSL is enabled:
// C#
client.ConnectSSL("mail.example.com");
' VB.NET
client.ConnectSSL("mail.example.com")
If you need to specify different port just use overloaded version of Connect method:
// C#
client.Connect("mail.example.com", 999);
' VB.NET
client.Connect("mail.example.com", 999)
If you are using SSL:
// 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();
Some servers require explicit SSL (Usually this triggers “The handshake failed due to an unexpected packet format” exception) please look at following articles for details:
If you have problems with SSL, your server may be using self-signed certificates:
Again when in doubt please ask your email server administrator for server, port, and SSL usage.