0

I'm trying to make SmtpClient send mails, but it always ends up with a timeout :(

The settings I use work in Mozilla Thunderbird, so I'm 100% sure the problem is in my code. I just cannot see where the problem is.

This is my code:

            MailMessage email = new MailMessage(fromAddress, to, subject, body);

            SmtpClient smtp = new SmtpClient();
            smtp.Host = HOSTNAME;
            smtp.Port = PORT;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(USERNAME, PASSWORD)
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            smtp.Timeout = 10000;

            smtp.Send(email);

Any ideas what might be going wrong?

The settings have been double checked to make sure they are correct. And I copied them from the code to Thunderbird when I tested sending mails in Thunderbird (i.e. I'm 100% sure the settings are correct).

Thanks!

Louisa
  • 552
  • 1
  • 9
  • 22

2 Answers2

1

It seems SmtpClient cannot handle SSL very well. (Discussed here: How can I send emails through SSL SMTP with the .NET Framework? and SmtpClient wont authenticate over SSL/TLS (not pointing to gmail))

You can either not to specify port and enablessl properties (the default values are 25, false), or use other SMTP clients.

Community
  • 1
  • 1
0

Since it's working in a thunderbird, then your credentials seems to work. Have you tried to remove timeout or create a try/catch around smtp.send to see the why it fails ?

farnholdt
  • 173
  • 9