2

In Windows Console Application with .NET 3.5 ( I changed the existing .NET 2.0 application to .NET 3.5 )

I have strange problem, the code for sending email works few times(may be 5 to 10 times).

After few times, it fails to send the email with message "Failure sending mail". The same code works after restarting the system. ( which is not the expected solution in production).

Here is the piece of code, I felt , somewhere I have close this SmtpClient Connection. so I set the client to null and called GC.Collect as well, but did not help me.

Please help



private static void SendEmail(MailMessage msg)
{
            SmtpClient client = new SmtpClient(GetSMTPServer(), GetSMTPPort());

            client.Credentials = CredentialCache.DefaultNetworkCredentials;
            client.EnableSsl = false;
            client.ServicePoint.MaxIdleTime = 1;
            //client.Timeout = GetSMTPTimeout(); 30000000
            client.Send(msg);
            client = null;
            GC.Collect();
}


Bennz
  • 21
  • 1
  • 2
  • 1
    The exception details is not enough, capture the details of exception. – Jahan Zinedine Dec 03 '12 at 18:32
  • - smEx {"Failure sending mail."} System.Net.Mail.SmtpException - InnerException {"Unable to read data from the transport connection: net_io_connectionclosed."} System.Exception {System.IO.IOException} – Bennz Dec 03 '12 at 19:16
  • check out http://stackoverflow.com/questions/1143846/unable-to-read-data-from-the-transport-connection-net-io-connectionclosed and similar questions – Jahan Zinedine Dec 03 '12 at 19:29
  • I have seen this post, it does not have the answers, other than upgrading to 4.0 and installing service packs. which cannot be recomended :( – Bennz Dec 03 '12 at 19:50
  • The correct way to free the resources used by your connection is with client.Finalize(true), not with client = null – Rafael Dec 03 '12 at 19:59
  • Hi Rafeel, this method does not seems to be supported by SmtpClient as the intellisence itself fails and unable to compile. This is the error message in the intellisense "System.Net.Mail.SmtpClient does not contain a definition and no extension method ......", cannot access protected method in the compile time error. probably I will have inherit and use this method. – Bennz Dec 03 '12 at 20:23
  • Hi Rafeel, this method does not seems to be supported by SmtpClient as the intellisence itself fails and unable to compile. This is the error message in the intellisense "System.Net.Mail.SmtpClient does not contain a definition and no extension method ......", cannot access protected method in the compile time error. probably I will have inherit and use this method. – Bennz Dec 03 '12 at 20:35
  • I don't believe you're supposed to call Finalize - use a `using` block as described in my answer. – jocull Oct 07 '13 at 14:12

2 Answers2

2

Try simply using a using block to properly dispose the SmtpClient after sending.

private static void SendEmail(MailMessage msg)
{
    using(SmtpClient client = new SmtpClient(GetSMTPServer(), GetSMTPPort()))
    {
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        client.EnableSsl = false;
        client.Send(msg);
    }
}

See also: .NET Best Method to Send Email (System.Net.Mail has issues)

Community
  • 1
  • 1
jocull
  • 20,008
  • 22
  • 105
  • 149
1

If the interval between two mail sending is bigger than 60 seconds and smaller than 90 seconds, you will get this error definitely.

for example, you send out the first email at 8:08:08 am, and then send out the second email at 8:09:10 am, an exception will be throw out.

it is a bug on SmtpClient.TimeOut settings, you can't change it.

how to solve this problem? three ways:

  1. try catch sending, if catch an exception, send again.
  2. SmtpClient.Dispose() for .net framework 3.0 and above
  3. set SmtpClient.servicepoint.maxidletime=1000; set to 1 is too small, not work in debug mode
Johann
  • 12,158
  • 11
  • 62
  • 89
Eric Zuo
  • 19
  • 2