我有以下 Email
类,其中包含 1 个方法 Send
:
using System.Net;
using System.Net.Mail;
static class Email
{
const string EmailAddress = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f9b3969cbb95969e9e8ab99d9694989097d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>";
const string Subject = "Error Detected in Application";
public static void Send(string body)
{
using (var client = new SmtpClient("mail.mailserevrname.com", 25)
{ Credentials = new NetworkCredential(), EnableSsl = false })
{
client.Send(EmailAddress,
EmailAddress,
Subject,
body);
}
}
}
我已在多个应用程序中使用此代码,并且从未遇到过任何问题(没有错误,电子邮件按预期/按预期发送)。
但是,在我当前的项目中,当我复制该类时,我看到以下错误:
'SmtpClient': type used in a using statement must be implicitly convertible to 'System.IDisposable'
我从未见过上述代码产生此错误。从the documentation也可以看出SmtpClient
确实实现了 IDisposable
:
公共(public)类 SmtpClient:IDisposable
有谁知道为什么现在出现此错误而不是在我的其他应用程序中?或者有人知道调查这个问题的一个好的起点是什么?
最佳答案
正如 @J.Steen 的评论所指出的,在 .NET Framework 3.5 中,SmtpClient
未实现 IDisposable
。
因此,解决方案是创建一个继承于 SmtpClient
并实现 IDisposable
的类,并在 using
中使用它。
编辑:
class Mailer: SmtpClient, IDisposable {
//...
}
是实现IDisposable
的方式,继承自SmtpClient
关于c# - Visual Studio 无法识别 IDisposable 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38631166/