Sending Image Embedded HTML Mail with C#

If you are not familiar with .Net Mail, have a look here.

First of all, create LinkedResource Object for the image or images; you want to embed in your Mail. Assign a unique name to ContentId Property of LinkedResource Object.


//Creating LinkedSource for embedded picture
LinkedResource myPhoto = new LinkedResource("c:\\27_n.jpg");
myPhoto.ContentId = "me";


Use this unique ContentId in < img src=cid:me > Tag to create a HTML AlternateView.


//Creating AlternateView for Plain Text Mail
AlternateView normalView = AlternateView.CreateAlternateViewFromString("Hello, Welcome to the party.", null, "text/plain");

//Creating AlternateView for HTML Mail
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<img src=cid:me>Hello, Welcome to the party.", null, "text/html");


Add LinkedResource Object to HTML AlternateView. You need to add AlternateViews to MailMessage as well.


//Adding LinkedSource to AlternateView
htmlView.LinkedResources.Add(myPhoto);

//Adding AlternateViews to MailMessage
mail.AlternateViews.Add(normalView);
mail.AlternateViews.Add(htmlView);


Image embedded Mail is ready to sent. The following one is complete code snippet to send image embedded mail.


MailMessage mail = new MailMessage();

SmtpClient smtpServer = new SmtpClient();

//SmtpClient Configuration
smtpServer.Credentials = new System.Net.NetworkCredential("khan.rahim@gmail.com", "password");
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;

//MailMessage Configuration
mail.From = new MailAddress("khan.rahim@gmail.com", "A Rahim Khan", System.Text.Encoding.UTF8);
mail.To.Add("rahim767@yahoo.com");
mail.Subject = "Mail with embedded image";

//Creating AlternateView for Plain Text Mail
AlternateView normalView = AlternateView.CreateAlternateViewFromString("Hello, Welcome to the party.", null, "text/plain");

//Creating AlternateView for HTML Mail
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<img src=cid:me>Hello, Welcome to the party.", null, "text/html");

//Creating LinkedSource for embedded picture
LinkedResource myPhoto = new LinkedResource("c:\\27_n.jpg");
myPhoto.ContentId = "me";

//Adding LinkedSource to AlternateView
htmlView.LinkedResources.Add(myPhoto);

//Adding AlternateViews to MailMessage
mail.AlternateViews.Add(normalView);
mail.AlternateViews.Add(htmlView);

mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.ReplyTo = new MailAddress("khan.rahim@gmail.com");

smtpServer.Send(mail);

Thanks
A Rahim Khan

Advertisements
  1. WOW, works like a charm, I am looking to use the gmail SMTP and I got what I want… thanks

    Regards, Rishi

    • CS
    • January 27th, 2012

    Will it work with windows form application too or with web application only?

    • Steve
    • January 19th, 2014

    Thanks for everything my friend. This,is exactly what I was looking for.
    I realy do not know how to thank you .This is just great. Thanks again

    • Steve
    • January 20th, 2014

    Hi guys. I’m trying to embed more images in the email but I can’t. I’m just able to embed one image. Can someone please show me how I can add more?. Thanks in advance

    • chriz
    • February 2nd, 2017

    How about retrieving the AlternateViews to MailMessage?

  1. June 15th, 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: