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

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

Definition

Namespace:

System.Net.Mail

Assemblies:

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

Warning

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

https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8

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”, “joey@friends.com”));

message.To.Add (new MailboxAddress (“joey@friends.com”, “alice@wonderland.com”));

message.To.Add (new MailboxAddress (“Alice”, “alice@wonderland.com”));

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 (“smtp.gmail.com“, 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=”jane@contoso.com,ben@contoso.com”

Then MailboxAddress will not parse it.

It should be converted to this format.

“jane@contoso.com”,

“ben@contoso.com”

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()

{

try

{

MailMessage mail = new MailMessage();

SmtpClient SmtpServerclient = new SmtpClient(“smtp.gmail.com“);

mail.From = new MailAddress(“your_email_address@gmail.com”);

mail.To.Add(“to_address”);

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;

SmtpServerclient.Send(mail);

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

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()

{

try

{

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(“smtp.gmail.com“, 465, SecureSocketOptions.SslOnConnect);

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

mail.From = new oldsmtp.MailAddress(“your_email_address@gmail.com”);

mail.To.Add(“your_email_address@gmail.com,your_email_address@gmail.com,your_email_address@gmail.com”);

mail.Subject = “Test Mail – 1”;

mail.IsBodyHtml = true;

string htmlBody;

htmlBody = “Write some HTML code here”;

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

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

Social media & sharing icons powered by UltimatelySocial