Sending HTML Mail with Embedded Image in .NET
page 6 of 10
by Soyuj Kumar Sahoo
Average Rating: 
Views (Total / Last 10 Days): 374294/ 95

A High priority mail with multi part bodies

This example will demonstrate how to create a Multi-Part mime message. Multi-Part messages are messages that contain alternate body parts (at least 2 alternate body parts). Alternate body parts are used for displaying different content to different mail clients (like those who supports HTML). Because it is impossible to sniff or determine an end user's mail client application, i.e. whether it supports for Html or not, we need to create different alternate body parts. Then it is up to the mail client to display the richest body part that they can render. 

Again, the priority of a message is controlled by headers; in the System.Net.Mail namespace, the Priority of a message is actually exposed as a property of the MailMessage Class.

A priority can have the following values from MailPriority enum of System.Net.Mail namespace: High, Low, and Normal.

The following example creates the most common Multi-Part mime email, a Plain Text and an HTML Text email (2 alternate body parts) with High Priority.

Listing 5 – Sample for a multi-part mail

Public Sub MultiPartMailBody()
        'create the mail message
        Dim mail As New MailMessage("""")
        'set the message header
        mail.Subject = " This is a Multipart mail with hight Priority"
        mail.Priority = MailPriority.High
        'first we create the Plain Text part
        Dim plainView As AlternateView =
        AlternateView.CreateAlternateViewFromString("This is my plain 
        text content, viewable by those clients that don't support html"Nothing,
        'then we create the Html part
        Dim htmlView As AlternateView =
        AlternateView.CreateAlternateViewFromString("<b>This is bold text, and
        viewable by those mail clients that support html</b>"Nothing,
        'send mail
End Sub ' End MultiPartMailBody

"PlainView" and "htmlView" are the instances of AlternateView Class, which represents the format to view an email message. CreateAlternateViewFromString creates multi part mime body from the content specified in string.

The MIME Source for above mail is:

Listing 6

x-recipient: <>
Received: xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
(###### #, #, #, #); Fri, 22 Jun 07 19:29:47 +0530
mime-version: 1.0
x-priority: 1
priority: urgent
importance: high
date: 22 Jun 2007 19:29:47 +0530
subject: This is a Multipart mail with high Priority
content-type: multipart/mixed; 
Message-Id: <f070704107052947xxxxxx>
X-Antivirus: AVG for E-mail 7.5.475 [269.9.4/860]
content-type: multipart/alternative; 
content-type: text/plain; charset=us-ascii
content-transfer-encoding: quoted-printable
This is my plain text content, 
viewable by those clients that don't support= html
content-type: text/html; charset=us-ascii
content-transfer-encoding: quoted-printable
<b>This is bold text, and viewable by those mail clients that support html</b>

The "boundary" of "content-type:" is the separator between two views of mail message.

View Entire Article

User Comments

Title: Attachment needed.   
Name: Chieko
Date: 2012-11-27 10:39:48 AM
I was so happy I could use this piece of code to add images to emails. The image did not show up in all of the emails until I added an attachment to the mail object.
' create email attachement.
Dim attach As New Net.Mail.Attachment(imagepath)
Name: weimin fu
Date: 2012-06-28 8:50:04 PM
The idea behind upgrading the world’s major tourist locales was to help travelers

better navigate otherwise unfamiliar areas, and probably to show off a little.

Structures like the Piazza del Duomo, that were once boxy buildings with some

dimension, now take on more dynamic shapes.
Title: Herve Leger Dress   
Name: Herve Leger Dress
Date: 2012-05-22 10:11:14 PM
It’s fun.Very worth to read.
Title: mail id send information   
Name: karthi
Date: 2012-05-16 3:04:31 AM
Date: 2012-04-27 11:20:38 PM
xyzboy6666 Designer sunglasses are also available which are designed by designers according to the customer's requirement. Every person likes to wear Designer sunglasses which enhance their personality. http://www.louisvuittonbeltscheap.comAs we know great personalities of world like to wear glasses according to their dress.
Title: work well   
Name: canada goose parka
Date: 2011-11-19 6:03:20 AM
just pass here,who interest winter jackets,can click my name,free ship and at least 70% discount,no interest ignore this messege.thanks
Title: Forwarding problem   
Name: Abhinay
Date: 2011-04-13 1:20:30 AM
I am facing problem while forwarding the mail.I have created mail as mention in article.It sends mail perfectly but when I forward that mail if does not contain the embedded image.
Title: it didnot work   
Name: CC
Date: 2011-04-04 3:48:33 PM

your code didnot working for me( IE8) et outlook 2007
i see the X
Title: Browser support   
Name: Sh
Date: 2011-04-02 8:29:27 AM

Does all client browser support embedded images in email.
Does the embedded image be displayed in gmail or outlook??
Title: Email Problem   
Name: Mohit Chauhan
Date: 2010-12-14 4:36:30 AM
actually my question is:
i want mail full capture page.when i send it then the page capture the pop up page.i am really confuse. how to capture previous page.

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htmlTw = new HtmlTextWriter(sw);

plz reply any one
mohit chauhan
Title: Well, you mention here things that really made me think.   
Name: ugg boots cheap
Date: 2010-11-26 10:19:13 AM
Well, you mention here things that really made me think.
Title: Sending HTML Mail with Embedded Image in .NET   
Name: Ramkumar
Date: 2010-11-18 10:30:37 AM
Hi Soyuj,
I tried with the same code and replaced the image location, From and To address values. But i am getting 'X' mark in place of image. But when i replace the HTML src with the original image location, the image is displayed in the mail. Kindly help me in solving this. I am using MS Outlook
Title: hi Anowar   
Name: prasath
Date: 2010-09-06 4:44:12 AM
how to embed image with background of TD tag ,
could you please send piece of code..
Title: Hotmail Problem   
Name: Anowar
Date: 2010-08-24 4:01:26 AM
I have used embedded image as background image of a TD tag and its working(showing) fine with yahoo email. But in hotmail it is not showing(background image).

Can you please suggest me what to do??
Title: hello sir   
Name: anup
Date: 2010-07-22 5:39:41 AM
thanks u for gudding me
Title: Thanks!   
Name: Fredrik
Date: 2010-07-19 4:37:24 PM
I was going crazy, not finding anything about how to embed a picture in my email. This post made my day. Thank you!
Title: email tracking   
Name: sagar
Date: 2010-06-04 5:39:09 AM
nice coding
Title: Example fail   
Name: David Pineda
Date: 2010-05-20 12:34:43 PM
Hi, unfortunely this sample fail, be cause the image is not present in the mail, just i can see square with 'X'.
Title: dwfgdsfg   
Name: dfgdsdfg
Date: 2010-04-14 3:28:22 PM
Title: I love the palin joke   
Name: mark
Date: 2010-02-22 1:23:17 PM
I assume you meant that as a joke..

Dim palinBody As String = "This is my plain text content, viewable by those clients that don't support html"
Title: ugg boots   
Name: ugg boots
Date: 2010-02-06 2:23:53 AM
I recently came across your blog and have been reading along.[url=]ugg[/url] I thought I would leave my first comment. [url=]ugg boots[/url] I don’t know what to say except that I have enjoyed reading.[url=]cheap ugg boots[/url] Nice blog.
Title: Different Way   
Name: Phil
Date: 2010-01-28 2:30:36 PM
Title: ugg boots   
Name: ugg boots
Date: 2009-12-24 7:48:59 PM
Thanks for the GREAT post. After fighting a bit with the image displaying in Hotmail, I came up with this code which seems to make it work
Title: Hotmail   
Name: Graham
Date: 2009-12-11 7:30:00 AM
Hi Pieter, could you post your entire code for image attachments for Hotmail. What about for multiple images? Would I need to create multiple image views?
Title: OWA, Hotmail, Yahoo and Googlemail   
Name: Pieter
Date: 2009-10-07 4:57:21 PM
Title: OWA   
Name: Pieter
Date: 2009-10-07 3:55:53 PM
My last comment did not include this line which breaks it for Exchange Web Access:
data.Name = "image2.jpg";

Title: Hotmail   
Name: Pieter
Date: 2009-10-07 3:34:16 PM
Thanks for the GREAT post. After fighting a bit with the image displaying in Hotmail, I came up with this code which seems to make it work:
Attachment data = new Attachment(imageView.ContentStream, MediaTypeNames.Image.Gif);
data.ContentDisposition.Inline = true;
data.ContentId = "uniqueId";
data.TransferEncoding = TransferEncoding.Base64;

Just use that instead of the message.AlternateViews.Add(imageView);

Hopefully this will help someone.
Title: So cool, thanks.   
Name: Richard
Date: 2009-07-02 2:56:31 PM
In stead of sending the embedded image email, How can we save it into the .eml template file?
Title: Error by using this codes   
Name: Nelson
Date: 2009-05-28 10:53:20 PM
dear friends
this is very useful for me, when i put this codes to my code it was getting error like follows

Type 'MailMessage' is not defined
i think i have done some mistake, please help me to solve this problem

Thanks in ADvanced
Dim mail As New MailMessage
Title: embedded Email   
Name: Suresh
Date: 2009-05-18 7:03:04 AM
Very Good Code. Thanks
Title: Works well, both google and yahoo   
Name: Sangam Uprety
Date: 2009-03-27 8:48:30 AM
Hi Soyuj!
Thanks for the descriptive tutorial.
This has worked for me smart!
I could get the images embedded in my mail which displayed well in google mail (gmail) and yahoo mail. But I could not test it now with hotmail.

The only problem is that I cannot get it displayed in the mail readers that use plain view. The result is awsome- the image link is being displayed, with cid:uniqueid as src of the image. Looks ugly.

With best regards,
Sangam Uprety
Title: This code is faulty   
Name: Vlax
Date: 2008-11-29 11:28:08 PM
Look for another example in the net.
The attached image doesn't come right, you need to use "relatedfiles" or something like that instead of creating a additional AlternateView
Title: Need urgent help   
Name: Gandhi Basnet
Date: 2008-11-22 11:16:37 PM
The code is working good now,
but the problem is when ever i am sending mail
the word "spam" is getting added to the subject
like if i send test it is like spam test in the subject line when i receive the mail

please help me out

Title: Works on Outlook Express but not for Outlook 2007   
Name: Gandhi Basnet
Date: 2008-11-20 3:58:33 AM
The embedded image is displayed in Outlook Express but not in Outlook 2007.
Please help me out.
Title: Thanks and a great job   
Name: Chris
Date: 2008-11-10 2:30:00 PM
I work in C#, and know little of VB.NET, but your code is just great and understandable still.

Title: need help urgent   
Name: Geani
Date: 2008-10-27 10:18:25 AM
I used same code. The only difference is the location of the image to be loaded. But, Image doesn't display properly.
It shows just an icon at the place of image. could u help me
Title: need help urgent   
Name: Rahul
Date: 2008-10-09 7:29:35 AM
It was a nice article.
But Image doesn't display properly.
it shows just a icon at the place of image
could u help me

Title: need help -urgent   
Name: sunil
Date: 2008-08-20 12:25:16 AM
i tried ur code its working good
but the problem is when ever i am sending mail
the word "spam" is getting added to the subject
like if i send test it is like spam test in the subject line when i receive the mail

please help me out

Title: great job   
Name: bunny
Date: 2008-08-18 10:16:20 PM
great work thanq very much for the post
i was working with web.mail some i got some issue with that
and ur code its really wow
thaks once again
Title: Gr8 buddy   
Name: Ankit Maini
Date: 2008-06-27 6:35:42 AM
Thanx buddy nice article for me.
Title: For Local Host   
Name: Prince Mathew
Date: 2008-02-19 11:23:33 PM
Thanks a lot... it saved much time...
Found some settings is needed to send thru localhost as server. Link provided will help
Title: View message before sending   
Name: Jerre
Date: 2008-01-30 9:23:19 AM
I there a way to view the message in outlook 2003 first before sending it?
Title: Thank You Very Much...   
Name: Slawek
Date: 2007-12-15 7:05:52 PM
Your article helped me very, very much and saved me much time. I think, you could add an examle how to authenticate on smtp server using login/password (single line at the end of this comment), and article will be truly complete

smtp.Credentials = new System.Net.NetworkCredential(login,password)
Title: Imran   
Name: Imran Ali
Date: 2007-11-06 10:56:14 AM
Hello ... Thank you ,,, It is very helpful
Title: ThankxxX   
Name: Mohammed. Akhtar
Date: 2007-08-29 6:18:45 AM
It was very useful.
Title: Thanx To You   
Name: Sai
Date: 2007-08-10 1:28:03 AM
Hello Sir,

I got good idea from this article but i expecting to know how can i read mails came to me in my gmail account in my application using POP3 Access.
Title: Pankaj   
Name: Pankaj Baviskar
Date: 2007-08-07 9:18:21 AM
Title: Very good work   
Name: Jose
Date: 2007-08-07 2:36:54 AM
Thank you Soyuj for this very good work. Each step is very well explained. Good job!
Title: Thanks!   
Name: Oscar
Date: 2007-08-06 11:53:30 AM
Thanks a lot Soyuj! i'll try it that way
Title: Hotmail issue..   
Name: Soyuj Kumar
Date: 2007-08-06 3:41:14 AM
Hello Oscar,

I have already checked with LinkedResource class and it is working fine for Hotmail.... try this..


Title: I am trying   
Name: Soyuj Kumar
Date: 2007-08-06 3:20:24 AM

Thanks for this nice questions!!! I have tried with Yahoo, Gmail, Hotmail... but got failed in Hotmail to see the image.. Yahoo and Gmail supports...

Please check the message source in all the experimental cases.. hello Kevin check the Outlook's source if the image source is there then we need to do some R&D..

BTW, another possible way is to try this action using "LinkedResource" which will take the path of the image.. and then (AlternateView)htmlView.add(LinkedResource) ....

So, please try with this...

Title: Thank You!   
Name: amin behzadi
Date: 2007-08-04 3:57:15 AM
Excelent...! Very nice and clear to understandig.
thank you!
Title: Question   
Name: Oscar
Date: 2007-08-03 11:20:02 AM
Excelent code Soyuj!! i have a question..the example works fine if i see the mail message in gmail(i see the text and the image), but in msnHotmail(i see the text and a gray box instead of the image) in mozilla i only see the image..
What could be the problem? is there any special setup that we need to see this mail correclty??
Thanks a lot!!!
Title: Thank, another question ask   
Name: Kevin
Date: 2007-08-03 2:43:25 AM
your code got worked in outlook express, failed in outlook.

in outlook, i just saw a red cross instead of the pic. Do you know why or have you tested it like that?

any ideas please feel free to contact me via thanks again.

Title: Thanks   
Name: Soyuj Kumar
Date: 2007-08-02 8:54:49 AM
Thanks !!! for these nice comments...
Title: Enjoyed your article   
Name: Patricia
Date: 2007-08-01 10:33:58 PM
Your writing style makes it so clear to understand. Thank you and looking forward to more of your articles.
Title: Nice work   
Name: John
Date: 2007-07-27 3:29:23 AM
Nice work man.. Keep it up !!!

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-18 4:03:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search