[英]Is System.Net.Mail.SmtpClient obsolete in 4.7?
Few days back I visited a blog that said System.Net.Mail.SmtpClient
is obsolete and an open source library MailKit and MimeKit is replacing it.几天前,我访问了一个博客,上面说
System.Net.Mail.SmtpClient
已经过时,一个开源库MailKit和MimeKit正在取代它。
I can see docs for that but not finding the same in reference code and in library.我可以看到相关文档,但在参考代码和库中找不到相同的文档。 Is it obsolete or not?
它是否已过时?
[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")]
public class SmtpClient : IDisposable
As Liam pointed out, this is obsolete due to a bug in documentation.正如 Liam 指出的那样,由于文档中的错误,这已经过时了。 Is System.Net.Mail.SmtpClient obsolete in 4.7?
System.Net.Mail.SmtpClient 在 4.7 中过时了吗?
... Microsoft has officially marked a .NET class as being replaced by an open source library.
... Microsoft 已正式将 .NET 类标记为已被开源库取代。 The documentation for
SmtpClient
now reads,SmtpClient
的文档现在读取,Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")
过时(“SmtpClient 及其类型网络设计不佳,我们强烈建议您使用https://github.com/jstedfast/MailKit和https://github.com/jstedfast/MimeKit ”)
The main problem with SmtpClient is that it has a confusing connection lifecycle.
SmtpClient 的主要问题是它具有令人困惑的连接生命周期。 Connecting to a SMTP server can be time-consuming, especially if authentication is enabled, so each
SmtpClient
object has an internal connection pool.连接到 SMTP 服务器可能很耗时,尤其是在启用身份验证的情况下,因此每个
SmtpClient
对象都有一个内部连接池。This is a rather strange design.
这是一个相当奇怪的设计。 Consider for a moment a typical database connection.
考虑一下典型的数据库连接。 When you call Dispose on a
SqlClient
, the underlying connection is returned to the pool.当您在
SqlClient
上调用 Dispose 时,底层连接将返回到池中。 When you create a newSqlClient
, the pool is checked for an active connection with the same connection string.创建新的
SqlClient
,会检查池中是否存在具有相同连接字符串的活动连接。With
SmtpClient
, callingDispose
closes all of the connections and drains that object's connection pool.使用
SmtpClient
,调用Dispose
关闭所有连接并耗尽该对象的连接池。 This means you can't use it with the typicalusing
block pattern .这意味着您不能将它与典型的
using
块模式一起using
。
A well-known approach of the shared instance like HttpClient
cannot be used in SmtpClient
.在
SmtpClient
不能使用像HttpClient
这样的众所周知的共享实例方法。
Unlike
HttpClient
, theSend
/SendAsync
methods are not thread thread-safe .与
HttpClient
不同,Send
/SendAsync
方法不是线程线程安全的。 So unless you want to introduce your own synchronization scheme, you can't use it that way either.所以除非你想引入你自己的同步方案,否则你也不能那样使用它。 In fact, the documentation for
SmtpClient
warns,事实上,
SmtpClient
的文档警告说,There is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.
无法确定应用程序何时完成使用 SmtpClient 对象并且应该对其进行清理。
By contrast, the SMTP client in MailKit represents a simple connection to a single server.
相比之下, MailKit 中的SMTP 客户端代表到单个服务器的简单连接。 By eliminating the complexity caused by internal connection pools, it actually makes it easier to create an application-specific pool for MailKit's connection object.
通过消除内部连接池带来的复杂性,实际上可以更轻松地为 MailKit 的连接对象创建特定于应用程序的池。
Ref: MailKit Officially Replaces .NET's SmtpClient参考: MailKit 正式取代 .NET 的 SmtpClient
As of today, SmtpClient in .NET Core 3.1 is usable but the official doc marks it as obsolete and recommends against using it (for OP's question, the answer is no, it wasn't obsolete in .NET Framework 4.7).截至今天, .NET Core 3.1 中的 SmtpClient可用,但官方文档将其标记为过时并建议不要使用它(对于 OP 的问题,答案是否定的,它在 .NET Framework 4.7 中并未过时)。
The SmtpClient class is obsolete in Xamarin.
SmtpClient 类在 Xamarin 中已过时。 However:
然而:
It is included in the .NET Standard 2.0 and later versions and therefore must be part of any .NET implementation that supports those versions.
它包含在 .NET Standard 2.0 和更高版本中,因此必须是支持这些版本的任何 .NET 实现的一部分。
It is present and can be used in .NET Framework 4 through .NET Framework 4.8.
它存在并且可以在 .NET Framework 4 到 .NET Framework 4.8 中使用。
It is usable in .NET Core, but its use isn't recommended.
它可用于 .NET Core,但不推荐使用。
I can't find any trace of System.Obsolete
inside .NET Framework 4.7.2
for SmtpClient class.对于 SmtpClient 类,我在
.NET Framework 4.7.2
找不到System.Obsolete
任何痕迹。 Does this mean that this class is no longer obsolete?这是否意味着该类不再过时?
Because in docs.microsoft.com it states clearly, This API is now obsolete.
因为在docs.microsoft.com 中明确指出,
This API is now obsolete.
EDIT:编辑:
SmtpClient is not obsolete, this is due to a bug in the documentation. SmtpClient 并未过时,这是由于文档中的错误所致。 See Issue here .
请参阅此处的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.