elmah / Elmah

Error Logging Modules & Handlers for ASP.NET
https://elmah.github.io/
Apache License 2.0
306 stars 65 forks source link

SmtpClient is not being correctly disposed of #399

Closed bartuszekj closed 8 years ago

bartuszekj commented 8 years ago

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);
}
atifaziz commented 8 years ago

@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?

bartuszekj commented 8 years ago

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

atifaziz commented 8 years ago

Merged & closed by 6861de61ddf80000e7204cd976418bd771921ccc

Thanks! Should be in the next service pack release.