简体   繁体   English

System.Net.Mail.SmtpClient 在 4.7 中过时了吗?

[英]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已经过时,一个开源库MailKitMimeKit正在取代它。

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 中过时了吗?

It is not obsolete in .NET Framework 4.7.它在 .NET Framework 4.7 中并未过时。 It was inadvertently documented as such in the API Browser due to a bug in the automated documentation generator.由于自动文档生成器中的错误,它在 API 浏览器中无意中被记录为这样。 However, it is obsolete in Mono and Xamarin.但是,它在单声道和Xamarin过时。

... 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/MailKithttps://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 new SqlClient , the pool is checked for an active connection with the same connection string.创建新的SqlClient ,会检查池中是否存在具有相同连接字符串的活动连接。

With SmtpClient , calling Dispose closes all of the connections and drains that object's connection pool.使用SmtpClient ,调用Dispose关闭所有连接并耗尽该对象的连接池。 This means you can't use it with the typical using block pattern .这意味着您不能将它与典型的using块模式一起using

A well-known approach of the shared instance like HttpClient cannot be used in SmtpClient .SmtpClient不能使用像HttpClient这样的众所周知的共享实例方法。

Unlike HttpClient , the Send / 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.

相关问题 如何在Xamarin中使用System.Net.Mail.SmtpClient发送邮件 - How to send a mail in Xamarin using System.Net.Mail.SmtpClient 第三方与System.Net.Mail.SmtpClient - Third-party vs System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient发送的电子邮件的字体大小 - Font size for an email sent using System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient将电子邮件发送到通讯组列表 - Sending email to a distribution list using System.Net.Mail.SmtpClient 发送到 System.Net.Mail.SmtpClient 等文件夹 - Send to folder like System.Net.Mail.SmtpClient 如何为电子邮件(System.Net.Mail.SmtpClient)附件设置一个好名称 - How to set a nice name for an e-mail (System.Net.Mail.SmtpClient) attachment 异步方法所需的响应时间与System.Net.Mail.SmtpClient的同步方法所需的时间差不多。 - Async method takes about same response time as sync method for System.Net.Mail.SmtpClient? System.Net.Mail.SMTPClient如何执行其本地IP绑定 - How does System.Net.Mail.SMTPClient do its local IP binding 如何使用TLS和System.Net.Mail.SMTPCLient连接到googlemail.com? - How can I connect to googlemail.com using TLS and System.Net.Mail.SMTPCLient? 使用System.Net.Mail.SmtpClient,更改SMTP FROM电子邮件地址 - Using System.Net.Mail.SmtpClient, change the SMTP FROM email address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM