所以我一直在使用System.Net.Mail.MailMessage
对象来发送电子邮件SmtpClient
一段时间了。我注意到某个地方MailMessage
实现了IDisposable
,所以我总是在一个using
块中使用它。
using(MailMessage msg = new MailMessage())
{
msg.To = blah... etc;
...
smtpclient.Send(msg);
}
从元数据中,您可以看到有关执行的信息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);
但我想知道,为什么MailMessage
实施IDisposable
?它似乎与网络相关的项目没有任何关系,因为SmtpClient
处理所有这些。
可能是由于可能持有附加文件的文件句柄吗?还有什么我忘记了吗?