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

}

}

color combination and create new color from two given color in .net

Lets discuss color capabilities in .net

Github link

For most people Color is RGB or ARGB

In.net

Color is a struct. This type provides a standard way to specify and mutate colors in the C# language. By adding a reference to the System.Drawing assembly, you can access this type and avoid writing your own color routines.

Colors can be represented as ARGB values, which store the alpha transparency, as well as the red, green and blue values. These values are stored as bytes which gives them a range of 0 to 255 inclusive.

So in order to make new color ( thats what this code does) suppose you have two colors color a and color b

Step 1

Please take the color a and  convert into RGB component via  Private Class colorcomponent

Step 2

do the same step with color b

now for individual R  take the average or fraction s if you are creating more than 2 color

Step 3

suppose R component is 100 for color a

suppose R component is 150 for color b

Step 4

then the middle color should have R value as 125

you may create 4 intermediate color in  that case  your will have R  as 110 120 130 140

Step 5 do same for G a nd B also and combine them to color back see colorcomponent class

2016-10-10_11-50-30

color-combination-demo-in-net

please download zip code file

Asp.net code

<p> How to use this class “colorcomponent” </p>
Dim colora As Color
colora = Color.Red
Dim colorb As Color
colorb = Color.Blue
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolor = 4
Dim outColor(numberofcolor + 1) As Color
ReDim outColor(numberofcolorinbetween + 1)
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolorinbetween + 1)

vb.net code for desktop application

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb))
ListBox1.Items.Add(ColorTranslator.ToHtml(colora))

End Sub
Private Class colorcomponent
Dim r As Integer
Dim g As Integer
Dim b As Integer
Dim a As Integer

Public Sub setcolor(C As Color)
Me.r = C.R
Me.g = C.G
Me.b = C.B
End Sub

Public Function getcolor(colorcomponent1 As colorcomponent) As Color
getcolor = Color.FromArgb(colorcomponent1.r Mod 256, colorcomponent1.g Mod 256, colorcomponent1.b Mod 256)

End Function
Public Function getcolorwithalpha(colorcomponent1 As colorcomponent, alpha As Byte) As Color
getcolorwithalpha = Color.FromArgb(alpha, CByte(colorcomponent1.r Mod 256), CByte(colorcomponent1.g Mod 256), CByte(colorcomponent1.b Mod 256))

End Function

Public Function getcolorarray(colorcomponent1 As colorcomponent, colorcomponent2 As colorcomponent, numberofcolorsneededinbetween As Double) As Color()

Dim outcolorcomponent As New colorcomponent()
Dim outColor(numberofcolorsneededinbetween) As Color
outColor(0) = getcolor(colorcomponent1)
If numberofcolorsneededinbetween > 0 Then
For i As Double = 1 To numberofcolorsneededinbetween – 1
If (colorcomponent1.r – colorcomponent2.r <> 0) Then
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.r = (colorcomponent1.r)
End If
If (colorcomponent1.g – colorcomponent2.g <> 0) Then
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.g = (colorcomponent1.g)
End If
If (colorcomponent1.b – colorcomponent2.b <> 0) Then
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)))
Else
outcolorcomponent.b = (colorcomponent1.b)
End If
outColor(i) = getcolor(outcolorcomponent)
Next
End If
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2)

Return outColor
End Function
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim colora As Color
colora = ColorTranslator.FromHtml(“#ff66ff”)
Dim colorb As Color
colorb = Color.Green
Dim coloracomponent As New colorcomponent()
Dim colorbcomponent As New colorcomponent()
coloracomponent.setcolor(colora)
colorbcomponent.setcolor(colorb)
Dim numberofcolors = 4
Dim outColor(numberofcolors + 1) As Color
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1)
ListBox1.Items.Clear()
Dim i = 1
For Each genfcolor As Color In outColor
Dim t As New TextBox()

Dim j = 100
‘ t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor
t.Text = ColorTranslator.ToHtml(genfcolor)
‘Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor))
‘ t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
Me.Controls.Add(t)
t.Location = New Point(8 * 50, 4 + i * 30)
Me.Show()
i += 2
Next

End Sub

End Class

c# code
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
private void Form1_Load(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
ListBox1.Items.Add(ColorTranslator.ToHtml(colorb));
ListBox1.Items.Add(ColorTranslator.ToHtml(colora));

}
private class colorcomponent
{
int r;
int g;
int b;

int a;
public void setcolor(Color C)
{
this.r = C.R;
this.g = C.G;
this.b = C.B;
}

public Color getcolor(colorcomponent colorcomponent1)
{
return Color.FromArgb(colorcomponent1.r % 256, colorcomponent1.g % 256, colorcomponent1.b % 256);

}
public Color getcolorwithalpha(colorcomponent colorcomponent1, byte alpha)
{
return Color.FromArgb(alpha, Convert.ToByte(colorcomponent1.r % 256), Convert.ToByte(colorcomponent1.g % 256), Convert.ToByte(colorcomponent1.b % 256));

}

public Color[] getcolorarray(colorcomponent colorcomponent1, colorcomponent colorcomponent2, double numberofcolorsneededinbetween)
{

colorcomponent outcolorcomponent = new colorcomponent();
Color[] outColor = new Color[numberofcolorsneededinbetween + 1];
outColor(0) = getcolor(colorcomponent1);
if (numberofcolorsneededinbetween > 0) {
for (double i = 1; i <= numberofcolorsneededinbetween – 1; i++) {
if ((colorcomponent1.r – colorcomponent2.r != 0)) {
outcolorcomponent.r = (colorcomponent1.r) + (((-colorcomponent1.r + colorcomponent2.r) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.r = (colorcomponent1.r);
}
if ((colorcomponent1.g – colorcomponent2.g != 0)) {
outcolorcomponent.g = (colorcomponent1.g) + (((-colorcomponent1.g + colorcomponent2.g) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.g = (colorcomponent1.g);
}
if ((colorcomponent1.b – colorcomponent2.b != 0)) {
outcolorcomponent.b = (colorcomponent1.b) + (((-colorcomponent1.b + colorcomponent2.b) * i / (numberofcolorsneededinbetween)));
} else {
outcolorcomponent.b = (colorcomponent1.b);
}
outColor(i) = getcolor(outcolorcomponent);
}
}
outColor(numberofcolorsneededinbetween) = getcolor(colorcomponent2);

return outColor;
}
}

private void Button1_Click(object sender, EventArgs e)
{
Color colora = default(Color);
colora = ColorTranslator.FromHtml(“#ff66ff”);
Color colorb = default(Color);
colorb = Color.Green;
colorcomponent coloracomponent = new colorcomponent();
colorcomponent colorbcomponent = new colorcomponent();
coloracomponent.setcolor(colora);
colorbcomponent.setcolor(colorb);
dynamic numberofcolors = 4;
Color[] outColor = new Color[numberofcolors + 2];
outColor = coloracomponent.getcolorarray(coloracomponent, colorbcomponent, numberofcolors + 1);
ListBox1.Items.Clear();
dynamic i = 1;
foreach (Color genfcolor in outColor) {
TextBox t = new TextBox();

dynamic j = 100;
// t.Location = New Point(t.Location.X + i, t.Location.Y + i)
t.ForeColor = genfcolor;
t.Text = ColorTranslator.ToHtml(genfcolor);
//Panel1.Controls.Add(t)
ListBox1.Items.Add(ColorTranslator.ToHtml(genfcolor));
// t.Location = New Point(ListBox1.Location.X + 100, ListBox1.Location.Y + i)
this.Controls.Add(t);
t.Location = new Point(8 * 50, 4 + i * 30);
this.Show();
i += 2;
}

}
public Form1()
{
Load += Form1_Load;
}

}