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.
I can se
... Microsoft has officially marked a .NET class as being replaced by an open source library. The documentation for
SmtpClient
now reads,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")
The main problem with SmtpClient is that it has a confusing connection lifecycle. Connecting to a SMTP server can be time-consuming, especially if authentication is enabled, so each
SmtpClient
object has an internal connection pool.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. When you create a newSqlClient
, the pool is checked for an active connection with the same connection string.With
SmtpClient
, callingDispose
closes all of the connections and drains that object's connection pool. This means you can't use it with the typicalusing
block pattern.
A well-known approach of the shared instance like HttpClient
cannot be used in SmtpClient
.
Unlike
HttpClient
, theSend
/SendAsync
methods are not thread thread-safe. So unless you want to introduce your own synchronization scheme, you can't use it that way either. In fact, the documentation forSmtpClient
warns,There is no way to determine when an application is finished using the SmtpClient object and it should be cleaned up.
By contrast, the SMTP client in MailKit represents a simple connection to a single server. 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.
Ref: MailKit Officially Replaces .NET's 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).
The SmtpClient class is obsolete in 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.
It is present and can be used in .NET Framework 4 through .NET Framework 4.8.
It is usable in .NET Core, but its use isn't recommended.
I can't find any trace of System.Obsolete
inside .NET Framework 4.7.2
for SmtpClient class. Does this mean that this class is no longer obsolete?
Because in docs.microsoft.com it states clearly, This API is now obsolete.
EDIT:
SmtpClient is not obsolete, this is due to a bug in the documentation. See Issue here.
As Liam pointed out, this is obsolete due to a bug in documentation. Is System.Net.Mail.SmtpClient obsolete in 4.7?
It is not obsolete in .NET Framework 4.7. It was inadvertently documented as such in the API Browser due to a bug in the automated documentation generator. However, it is obsolete in Mono and Xamarin.