So I’ve been using System.Net.Mail.MailMessage objects for sending e-mail via SmtpClient for a while now. I noticed somewhere that MailMessage implements IDisposable, so I always use it within a using block.
using(MailMessage msg = new MailMessage())
{
msg.To = blah... etc;
...
smtpclient.Send(msg);
}
From metadata, you can see this info on the implementation of MailMessage
// Summary:
// Releases all resources used by the System.Net.Mail.MailMessage.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Dispose();
//
// Summary:
// Releases the unmanaged resources used by the System.Net.Mail.MailMessage
// and optionally releases the managed resources.
//
// Parameters:
// disposing:
// true to release both managed and unmanaged resources; false to release only
// unmanaged resources.
protected virtual void Dispose(bool disposing);
But I’m wondering, why does MailMessage implement IDisposable? It does not appear to have anything to do with network-related items, because the SmtpClient handles all that.
Could it be due to potentially holding file handles for attached files? Is there something else there I’m forgetting?
According to dotPeek, it is disposing of its attachments and its views: