Sending HTML Mail with Embedded Image in .NET
page 5 of 10
by Soyuj Kumar Sahoo
Feedback
Average Rating: 
Views (Total / Last 10 Days): 293148/ 8002

A simple HTML mail

Let us check a mail message created by System.Net.Mail.MailMessage to send a default mail.

Listing 1 – Sample for plain mail

Public Sub SendPlainMail()
 
  'create the mail message
  Dim mail As New MailMessage("from@fromdomain.com""to@todomain.com")
 
  'set the message content
  mail.Subject = "This is a plain text mail"
  mail.Body = "This is a sample body... for default Mail Clients.."
 
  'send the mail using SMTP Client
  Dim smtp As New SmtpClient("My Mail Server"'mail Server IP or NAME 
  smtp.Send(mail)
End Sub ' End SendPlainMail

Here, "mail" is the instance of MailMessage Class. There are four overloaded constructors available initializing different properties of MailMessage Class during making instance.

·         MailMessage (): Initializes an empty instance of the MailMessage class. 

·         MailMessage (from as MailAddress, to as MailAddress): Initializes a new instance of the   MailMessage class by using from and to as MailAddress class objects. 

·         MailMessage (from as String, to as String): Initializes a new instance of the MailMessage class by using from and to as String objects. 

·         MailMessage (from as String, to as String, subject as String, body as String): Initializes a new instance of the MailMessage class by using from, to, subject and body as String objects. 

Again, "smtp" is the instance of SmtpClient Class with Mail Server as parameter, which does the actual mail sending action. There are three available constructors of SmtpClient Class as:

·         SmtpClient (): Initializes a new instance of the SmtpClient class.

·         SmtpClient (host as String): Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server (host). 

·         SmtpClient (host as String, port as Int32): Initializes a new instance of the SmtpClient class that sends email by using the specified SMTP server and port. 

The MIME Source for above mail is:

Listing 2

'--------------------------------
x-recipient: <to@todomain.com>
Received: xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
(###### #, #, #, #); Fri, 22 Jun 07 18:53:37 +0530
mime-version: 1.0
from: from@fromdomain.com
to: to@todomain.com
date: 22 Jun 2007 18:53:37 +0530
subject: This is a plain text mail
content-type: text/plain; charset=us-ascii
content-transfer-encoding: quoted-printable
Message-Id: <f0706221908020DDAxxxxxx>
X-Antivirus: AVG for E-mail 7.5.472 [269.9.4/860]
This is a sample body... for default Mail Clients..
'--------------------------------

Here, the "content-type" field value is "text/plain," i.e. content-type: text/plain, which specifies that this Mail Client (Outlook or Eudora) is a plain mail only.

By default the mail message created by System.Net.Mail is a plain text, whose default value for   the Public Property MailMessage.IsBodyHtml is False. To format a message as Html we need to set this property MailMessage.IsBodyHtml property to True.

Listing 3 – Sample for HTML mail

Public Sub SendHtmlMail()
 
  'create the mail message
  Dim mail As New MailMessage("from@fromdomain.com""to@todomain.com")
 
   'set the message content
   mail.Subject = "This mail has Html Body.."
   mail.Body = "This is a sample body with html in it. <b>This is bold</b> <font
   color=#336699>This is blue</font>"
   mail.IsBodyHtml = True
   'send mail
   SendMail(mail)
End Sub ' End SendHtmlMail
 
Private Sub SendMail(ByVal mail As Mail.MailMessage)
  'send the message using SMTP client
  Dim smtp As New SmtpClient(_mailServer) 'mail Server IP or NAME
  smtp.Credentials = CredentialCache.DefaultNetworkCredentials
  smtp.Send(Mail)
End Sub ' End SendMail

The MIME Source for above mail is:

Listing 4 – Mime source of HTML mail

'--------------------------------
x-recipient: <to@todomain.com>
Received: xxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxx
(###### #, #, #, #); Fri, 22 Jun 07 18:59:12 +0530
mime-version: 1.0
from: from@fromdomain.com
to: to@todomain.com
date: 22 Jun 2007 18:59:12 +0530
subject: This mail has Html Body..
content-type: text/html; charset=us-ascii
content-transfer-encoding: quoted-printable
Message-Id: <f0707041437531C0Axxxxxx>
X-Antivirus: AVG for E-mail 7.5.475 [269.9.4/860]
This is a sample body with html in it. <b>This is bold</b> 
<font color=3D#336699>This=is blue </font>
'--------------------------------

The "content-type" field value is "text/html" i.e. content-type: text/html, which specifies to the Mail Client (Outlook or Eudora) that this mail contains html content.


View Entire Article

User Comments

Title: Attachment needed.   
Name: Chieko
Date: 11/27/2012 10:39:48 AM
Comment:
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)
Title: www.hairsshop.co.uk   
Name: weimin fu
Date: 6/28/2012 8:50:04 PM
Comment:
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: 5/22/2012 10:11:14 PM
Comment:
It’s fun.Very worth to read.
Title: mail id send information   
Name: karthi
Date: 5/16/2012 3:04:31 AM
Comment:
ss
Title: www.cheapguccibeltoutlet.com   
Name: www.cheapguccibeltoutlet.com
Date: 4/27/2012 11:20:38 PM
Comment:
xyzboy6666 Designer sunglasses are also http://www.cheapguccibeltoutlet.com available which are designed by designers according to the customer's requirement.http://www.cheapsunglassesexport.com 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: 11/19/2011 6:03:20 AM
Comment:
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: 4/13/2011 1:20:30 AM
Comment:
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: 4/4/2011 3:48:33 PM
Comment:
Hello

your code didnot working for me( IE8) et outlook 2007
i see the X
Title: Browser support   
Name: Sh
Date: 4/2/2011 8:29:27 AM
Comment:
Hi

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: 12/14/2010 4:36:30 AM
Comment:
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);
base.Render(htmlTw);


plz reply any one
mohit chauhan
mohit@purplehazeonline.com
mohit.chauhan8@gmail.com
Title: Well, you mention here things that really made me think.   
Name: ugg boots cheap
Date: 11/26/2010 10:19:13 AM
Comment:
Well, you mention here things that really made me think.
Title: Sending HTML Mail with Embedded Image in .NET   
Name: Ramkumar
Date: 11/18/2010 10:30:37 AM
Comment:
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: 9/6/2010 4:44:12 AM
Comment:
how to embed image with background of TD tag ,
could you please send piece of code..

prasath.r@3rd-wave.com
Title: Hotmail Problem   
Name: Anowar
Date: 8/24/2010 4:01:26 AM
Comment:
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: 7/22/2010 5:39:41 AM
Comment:
thanks u for gudding me
Title: Thanks!   
Name: Fredrik
Date: 7/19/2010 4:37:24 PM
Comment:
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: 6/4/2010 5:39:09 AM
Comment:
nice coding
Title: Example fail   
Name: David Pineda
Date: 5/20/2010 12:34:43 PM
Comment:
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: 4/14/2010 3:28:22 PM
Comment:
Cool
Title: I love the palin joke   
Name: mark
Date: 2/22/2010 1:23:17 PM
Comment:
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: 2/6/2010 2:23:53 AM
Comment:
I recently came across your blog and have been reading along.[url=http://www.sellcheapuggs.com]ugg[/url] I thought I would leave my first comment. [url=http://www.sellcheapuggs.com]ugg boots[/url] I don’t know what to say except that I have enjoyed reading.[url=http://www.sellcheapuggs.com]cheap ugg boots[/url] Nice blog.
Title: Different Way   
Name: Phil
Date: 1/28/2010 2:30:36 PM
Comment:
\
Title: ugg boots   
Name: ugg boots
Date: 12/24/2009 7:48:59 PM
Comment:
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: 12/11/2009 7:30:00 AM
Comment:
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: 10/7/2009 4:57:21 PM
Comment:
\
Title: OWA   
Name: Pieter
Date: 10/7/2009 3:55:53 PM
Comment:
My last comment did not include this line which breaks it for Exchange Web Access:
data.Name = "image2.jpg";

Sorry...
Title: Hotmail   
Name: Pieter
Date: 10/7/2009 3:34:16 PM
Comment:
Hi
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;
message.Attachments.Add(data);

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

Hopefully this will help someone.
Title: So cool, thanks.   
Name: Richard
Date: 7/2/2009 2:56:31 PM
Comment:
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: 5/28/2009 10:53:20 PM
Comment:
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: 5/18/2009 7:03:04 AM
Comment:
Very Good Code. Thanks
Title: Works well, both google and yahoo   
Name: Sangam Uprety
Date: 3/27/2009 8:48:30 AM
Comment:
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
http://dotnetspidor.blogspot.com
Title: This code is faulty   
Name: Vlax
Date: 11/29/2008 11:28:08 PM
Comment:
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: 11/22/2008 11:16:37 PM
Comment:
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

thanks
Title: Works on Outlook Express but not for Outlook 2007   
Name: Gandhi Basnet
Date: 11/20/2008 3:58:33 AM
Comment:
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: 11/10/2008 2:30:00 PM
Comment:
I work in C#, and know little of VB.NET, but your code is just great and understandable still.

Congratulations!
Title: need help urgent   
Name: Geani
Date: 10/27/2008 10:18:25 AM
Comment:
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: 10/9/2008 7:29:35 AM
Comment:
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

Thanks
Title: need help -urgent   
Name: sunil
Date: 8/20/2008 12:25:16 AM
Comment:
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

thanks
Title: great job   
Name: bunny
Date: 8/18/2008 10:16:20 PM
Comment:
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: 6/27/2008 6:35:42 AM
Comment:
Thanx buddy nice article for me.
Title: For Local Host   
Name: Prince Mathew
Date: 2/19/2008 11:23:33 PM
Comment:
Thanks a lot... it saved much time...
Found some settings is needed to send thru localhost as server. Link provided will help

http://codebetter.com/blogs/peter.van.ooijen/archive/2006/04/05/Using-localhost-as-mailserver-_2800_5.7.1-Unable-to-relay-for-xxx_2900_.aspx
Title: View message before sending   
Name: Jerre
Date: 1/30/2008 9:23:19 AM
Comment:
I there a way to view the message in outlook 2003 first before sending it?
Title: Thank You Very Much...   
Name: Slawek
Date: 12/15/2007 7:05:52 PM
Comment:
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
Slawek

smtp.Credentials = new System.Net.NetworkCredential(login,password)
Title: Imran   
Name: Imran Ali
Date: 11/6/2007 10:56:14 AM
Comment:
Hello ... Thank you ,,, It is very helpful
Title: ThankxxX   
Name: Mohammed. Akhtar
Date: 8/29/2007 6:18:45 AM
Comment:
It was very useful.
Title: Thanx To You   
Name: Sai
Date: 8/10/2007 1:28:03 AM
Comment:
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 asp.net application using POP3 Access.
Title: Pankaj   
Name: Pankaj Baviskar
Date: 8/7/2007 9:18:21 AM
Comment:
\
\
Title: Very good work   
Name: Jose
Date: 8/7/2007 2:36:54 AM
Comment:
Thank you Soyuj for this very good work. Each step is very well explained. Good job!
Title: Thanks!   
Name: Oscar
Date: 8/6/2007 11:53:30 AM
Comment:
Thanks a lot Soyuj! i'll try it that way
Title: Hotmail issue..   
Name: Soyuj Kumar
Date: 8/6/2007 3:41:14 AM
Comment:
Hello Oscar,

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

Thanks.

Soyuj
Title: I am trying   
Name: Soyuj Kumar
Date: 8/6/2007 3:20:24 AM
Comment:
Hi...

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...

Thanks,
Soyuj
Title: Thank You!   
Name: amin behzadi
Date: 8/4/2007 3:57:15 AM
Comment:
Excelent...! Very nice and clear to understandig.
thank you!
Title: Question   
Name: Oscar
Date: 8/3/2007 11:20:02 AM
Comment:
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: 8/3/2007 2:43:25 AM
Comment:
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 kevins@verdant.com.au. thanks again.

Cheers,
Kevin
Title: Thanks   
Name: Soyuj Kumar
Date: 8/2/2007 8:54:49 AM
Comment:
Thanks !!! for these nice comments...
Title: Enjoyed your article   
Name: Patricia
Date: 8/1/2007 10:33:58 PM
Comment:
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: 7/27/2007 3:29:23 AM
Comment:
Nice work man.. Keep it up !!!

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/25/2014 8:15:56 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search