elmah/Elmah

SmtpClient is not being correctly disposed of

Closed this issue · 3 comments

SmtpClient implements IDisposable as of .NET 4.0. ErrorMailModule.SendMail method does not correctly dispose of its SmtpClient. This causes the smtp connection to hang for an extended amount of time. Added benefit of properly closing/disposing of the SmtpClient instance is that an smtp QUIT command can be properly sent to the server.

To maintain compatibility between .NET versions, I suggest implementing the following solution:

var client = new SmtpClient();

using (client as IDisposable) // allow client not to implement IDisposable, but correctly handle the 4.0+ implementation
{
   // ... do all the SmtpClient work
   client.Send(mail);
}

@bartuszekj Thanks for bringing this up! Do you want to try and submit a PR against the 1x branch with the compatible change you proposed?

Done. It did create a new issue for this, though (#401).

Merged & closed by 6861de6

Thanks! Should be in the next service pack release.