How to replace/update code for System.Net.Mail SmtpClient Class with Mailkit SmtpClient Class

As most of us have seen this warning.

SmtpClient Class





System.dll, netstandard.dll, System.Net.Mail.dll


This API is now obsolete.

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient type is now obsolete.

Microsoft reference Link

MimeKit and MailKit are popular fully-featured email frameworks for .NET.

So how to convert you code from Old format to new.

Adding MailKit to your project via NuGet

· In Visual Studio‘s Package Manager Console, enter the following command:

· Install-Package MailKit

If you are writing a new code its good to use samples provided on Mailkit site.

Mailkit code

var message = new MimeMessage ();

message.From.Add (new MailboxAddress (“Joey”, “”));

message.To.Add (new MailboxAddress (“”, “”));

message.To.Add (new MailboxAddress (“Alice”, “”));

message.Subject = “How you doin?”;

message.Body = new TextPart (“html”) {

Text = @”Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on

Saturday and I was hoping you could make it.

Will you be my +1?

— Joey


public static void SendMessages (IList<MimeMessage> messages)


using (var client = new SmtpClient ()) {

client.Connect (““, 465, SecureSocketOptions.SslOnConnect);

client.Authenticate (“username”, “password”);

foreach (var message in messages) {

client.Send (message);


client.Disconnect (true);



If you have old code then follow these easy conversions.

Now check your old code

If you have “Recipientlist” value=”,”

Then MailboxAddress will not parse it.

It should be converted to this format.



You will note that the MimeMessage body is no more of String type (TextPart).

Sample code for conversion.

using System.Net.Mail;

private void sendemailclick()




MailMessage mail = new MailMessage();

SmtpClient SmtpServerclient = new SmtpClient(““);

mail.From = new MailAddress(“”);


mail.Subject = “Test Mail – 1”;

mail.IsBodyHtml = true;

string htmlBody;

htmlBody = “Write some HTML code here”;

mail.Body = htmlBody;

SmtpServerclient.Port = 587;

SmtpServerclient.Credentials = new System.Net.NetworkCredential(“username”, “password”);

SmtpServerclient.EnableSsl = true;



catch (Exception ex)





This is the method which comes handy

Importing from System.Net.Mail

To make things even simpler still, MimeKit allows you to explicitly cast a MailMessage to a MimeMessage

Since Smtpclient is  obsolete but not the mail message, We
can still leverage MailMessage. It had very great
parse method for email address separated with comma and mail body as string.

using MailKit.Net.Smtp;

using MimeKit;

using System.Text;

using oldsmtp=System.Net.Mail

using MailKit.Security;

private void sendemailclick()




oldsmtp.MailMessage mail = new oldsmtp.MailMessage();

SmtpClient SmtpServerclient = new SmtpClient();

//this is needed if certificate is bad or self signed.

SmtpServerclient.ServerCertificateValidationCallback = (s, c, h, e) => true;

SmtpServerclient.Connect(““, 465, SecureSocketOptions.SslOnConnect);

SmtpServerclient.Authenticate(“username”, “password”);

mail.From = new oldsmtp.MailAddress(“”);


mail.Subject = “Test Mail – 1”;

mail.IsBodyHtml = true;

string htmlBody;

htmlBody = “Write some HTML code here”;

SmtpServerclient .Send(MimeMessage.CreateFromMailMessage(mail));


catch (Exception ex)





Social media & sharing icons powered by UltimatelySocial