Click here to Skip to main content
13,053,844 members (57,531 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 7 Feb 2012

How to embed an image in email body

Rate this:
Please Sign up or sign in to vote.
How to embed an image in email body

Usually when we are sending images in an email, we place the...

<img src="" alt="" />

...tag in the HTML email body and the 'src' of the image tag points to the HTTP URL of the image. To get this HTTP URL, the image has to be hosted somewhere, either on your own website or somewhere on the internet.

But in many scenarios, you don't have the HTTP URL to the image and you want to send the image in an email.

This kind of scenario occurs while sending images that are saved in the database or when sending the emails from a Windows application.

In these scenarios, you need to use the LinkedResource object to directly 'embed' the image in an HTML email and then send the email using our standard .NET 'MailMessage' class.

Here is the complete code to it - tested and working:

MailMessage Mail = new MailMessage();        

Mail.From = new MailAddress("");
Mail.Subject = "This is Image Test.";
Mail.Body = "This is the body of the email";
LinkedResource LinkedImage = new LinkedResource(@"J:\My Documents\Advika1.jpg");
LinkedImage.ContentId = "MyPic";
//Added the patch for Thunderbird as suggested by Jorge
LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
  "You should see image next to this line. <img src=cid:MyPic>", 
  null, "text/html");

SmtpClient smtp = new SmtpClient("", 25); 
catch (SmtpException ex)

Let me know if this post helps you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balaji Birajdar (India)
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.

Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.

I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .

I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

You may also be interested in...

Comments and Discussions

AnswerNice article but need more details Pin
demonhealer30-Jun-15 19:34
memberdemonhealer30-Jun-15 19:34 
QuestionOutstanding! Pin
spencepk25-Jul-14 5:58
memberspencepk25-Jul-14 5:58 
QuestionImage from an url Pin
ankurtrue14-Dec-13 0:46
memberankurtrue14-Dec-13 0:46 
QuestionDecent, but .. Pin
Darek Danielewski2-Dec-13 10:17
memberDarek Danielewski2-Dec-13 10:17 
AnswerRe: Decent, but .. Pin
spencepk25-Jul-14 5:03
memberspencepk25-Jul-14 5:03 
GeneralRe: Decent, but .. Pin
Darek Danielewski12-Aug-14 8:54
memberDarek Danielewski12-Aug-14 8:54 
GeneralThank you Jorge for the update. I will include it in my tip.... Pin
B Birajdar19-Feb-12 19:40
memberB Birajdar19-Feb-12 19:40 
GeneralFive Man! Thunderbird not show any image embedded. Only a b... Pin
Jorge Valencia17-Feb-12 10:08
memberJorge Valencia17-Feb-12 10:08 
GeneralThank you Jawed, skantg, drewman, Tushar ..Its the appreciat... Pin
B Birajdar15-Feb-12 22:42
memberB Birajdar15-Feb-12 22:42 
GeneralReason for my vote of 5 Good Work Pin
Tushar_Patil15-Feb-12 0:14
memberTushar_Patil15-Feb-12 0:14 
GeneralReason for my vote of 5 Nice one! This issue has had me scra... Pin
drewman51509-Feb-12 1:10
memberdrewman51509-Feb-12 1:10 
GeneralReason for my vote of 5 a good tip, often needed. Pin
skantg8-Feb-12 21:30
memberskantg8-Feb-12 21:30 
GeneralReason for my vote of 4 Nice tips..i have seen loads of user... Pin
jawed.ace7-Feb-12 1:35
memberjawed.ace7-Feb-12 1:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 Dec 2013
Article Copyright 2012 by Balaji Birajdar (India)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid