Click here to Skip to main content
13,356,787 members (73,017 online)
Click here to Skip to main content
Add your own
alternative version


83 bookmarked
Posted 26 Sep 2005

Embed HTML Email Images

, 5 Jun 2007
Rate this:
Please Sign up or sign in to vote.
How to embed images in your email newsletter.

Other projects you will need to download

Screenshot - PSH_Ne2.png


How to embed images into an email; short and sweet, but this is all this does.


My sister wanted to send out an email newsletter and wanted to embed images. So, she asked a friend and they sent her some classic ASP code. Then she asked me to make it work. Yeah, right! I fired up VS 2003 and got a hold of MIL HTML Parser and DotNetOpenMail. Within an hour, I had a small little app that could take a webpage, add all the needed images, and change the img tags to point to the content IDs of the images attached to the email and send it off to an email address.

Using the code

This code is really straightforward. Just select an HTML file that you have created, add a subject, a "from" email address, a "to" email address and the SMTP server name or IP address, and then click Send. But what does the code do? Well, you can't get much simpler than this. The code goes off and gets all the img elements.

// Get All the img nodes
private void GetImageNodes(HtmlNodeCollection nodes)
    foreach (HtmlNode node in nodes)
        HtmlElement element = node as HtmlElement;
        if (element != null)
            if (element.Name.ToLower() == "img")
            if (element.Nodes.Count > 0)

Then all the needed images are attached and the src attributes of the img elements are set to the content ID of the attached images.

// Change all the img nodes
foreach (HtmlElement element in imageNodes)
    FileInfo imageFileInfo = new FileInfo(Path.Combine(
    string contentId =

    if (!images.ContainsKey(element.Attributes["src"].Value))
        // Add Image to the Email
            element.Attributes["src"].Value, imageFileInfo.FullName);
        FileAttachment relatedFileAttachment =
                       new FileAttachment(imageFileInfo, contentId);
        if ((imageFileInfo.Extension == ".jpg") ||
            (imageFileInfo.Extension == ".jpeg"))
            relatedFileAttachment.ContentType = "image/jpeg";
        else if (imageFileInfo.Extension == ".gif")
            relatedFileAttachment.ContentType = "image/gif";

    //Change the src to "cid:<CONTENTID>"
    element.Attributes["src"].Value = string.Format("cid:{0}", contentId);


And then the changed HTML is added to the email message and sent off.

// set the email text to the changes html
emailMessage.HtmlPart = new HtmlAttachment(document.HTML);

emailMessage.Send(new SmtpServer(this.SmtpServerTextBox.Text));

Points of interest

I just slapped this code together in an hour. In fact, writing the article added another 30 minutes. So, this is not code to look to for best practices. I just wanted to embed images into an email with a minimum of hassle and I achieved that goal with this. I hope others will find it useful.


  • 5/6/2007 - Version 2.0
  • 27/9/2005 - Version 1.0 - Also happens to be my birthday! :D


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Grosvenor Financial Services Group Ltd.
New Zealand New Zealand
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionI have an Outlook Integration (without using Exchange) Pin
Philip Carter16-Oct-11 7:39
memberPhilip Carter16-Oct-11 7:39 
Generalweb interface html to eml Pin
pieruigi12-Oct-10 11:30
memberpieruigi12-Oct-10 11:30
good article!

if someone need a web go here:
QuestionSSL [modified] Pin
Ivica7619-Sep-10 23:51
memberIvica7619-Sep-10 23:51 
QuestionSPAM problem Pin
pinokar2-Jul-07 6:57
memberpinokar2-Jul-07 6:57 
AnswerRe: SPAM problem Pin
mwdiablo19-Jul-07 13:42
membermwdiablo19-Jul-07 13:42 
GeneralYou can also... Pin
axelriet5-Jun-07 14:47
memberaxelriet5-Jun-07 14:47 
GeneralRe: You can also... Pin
mwdiablo9-Jun-07 17:53
membermwdiablo9-Jun-07 17:53 
GeneralAn Easy Way... Pin
merlin9815-Jun-07 10:14
membermerlin9815-Jun-07 10:14 
GeneralRe: An Easy Way... Pin
mwdiablo19-Jul-07 13:43
membermwdiablo19-Jul-07 13:43 
Questionproblem creating NewsLetter.... Pin
mohnish.bilimoria31-May-07 23:52
membermohnish.bilimoria31-May-07 23:52 
Generaldon't want to use SmtpServer for sending newsletter Pin
farooqahm14-May-07 2:21
memberfarooqahm14-May-07 2:21 
GeneralRe: don't want to use SmtpServer for sending newsletter Pin
mwdiablo20-May-07 23:50
membermwdiablo20-May-07 23:50 
GeneralimageNodes error Pin
Skip6710-Dec-06 7:41
memberSkip6710-Dec-06 7:41 
GeneralRe: imageNodes error Pin
mwdiablo20-May-07 23:48
membermwdiablo20-May-07 23:48 
GeneralEmbedd Flash Pin
SaarDagan22-Nov-06 4:20
memberSaarDagan22-Nov-06 4:20 
GeneralRe: Embedd Flash Pin
mwdiablo20-May-07 23:47
membermwdiablo20-May-07 23:47 
Generalcodsys.dll Pin
jbrathwaite9-Aug-06 19:52
memberjbrathwaite9-Aug-06 19:52 
AnswerRe: codsys.dll Pin
mwdiablo9-Aug-06 22:55
membermwdiablo9-Aug-06 22:55 
Generalhelp Pin
beckymasue1-Aug-06 5:16
memberbeckymasue1-Aug-06 5:16 
AnswerRe: help Pin
mwdiablo1-Aug-06 14:39
membermwdiablo1-Aug-06 14:39 
Questionbackground images Pin
tianmingkun23-May-06 1:02
membertianmingkun23-May-06 1:02 
AnswerRe: background images Pin
mwdiablo4-Jun-06 16:56
membermwdiablo4-Jun-06 16:56 
Generalthanks Pin
drkwtkns4-Oct-05 7:47
memberdrkwtkns4-Oct-05 7:47 
GeneralRe: thanks Pin
mwdiablo4-Oct-05 10:44
membermwdiablo4-Oct-05 10:44 
Generaleven birthday to me.... Pin
jreddy26-Sep-05 17:37
memberjreddy26-Sep-05 17:37 

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
Web01 | 2.8.180111.1 | Last Updated 5 Jun 2007
Article Copyright 2005 by KiwiPiet
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid