Vasu Balakrishnan’s Blog

SmtpClient Connection Timeout

leave a comment »

I was experiencing a strange behavior within my WPF Application when I was trying to send an email using SmtpClient / MailMessage. Nothing fancy here. The application sends out an email with attachment to a list of recipients.

Here is the strange behavior. Even after calling Send method, nothing happens. I wait there for few minutes, I don’t see the email being sent out and also I don’t see it in my inbox. As soon as I close my application, I receive the email in my inbox. This was a consistent behavior. I didn’t know what was causing it. After looking around, I came to know that it could be a Smtp Connection Timeout issue. I didn’t see any such properties in SmtpClient object. After spending sometime on it, I saw SmtpClient exposes a ServicePoint object, which has the answer to my problem. ServicePoint gets the underlying network connection used to transmit the email message.

ServicePoint exposes ConnectionLeaseTimeout property, which I’d to set to resolve my issue. After reading the documentation, it turns out that the default behavior is to allow the active ServicePoint connection to stay connected indefinitely. Once I set a value, the connection gets closed and I get the email. Nicey nice 🙂

Written by nbvasu

May 14, 2009 at 3:19 am

Posted in C#

Tagged with

Leave a comment