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 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
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 web service to use TLS 1.2.
The System. Net.Mail classes are contained in the "System. dll" file. Make sure you have a reference to System listed under References in your project.
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.
... 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With