Click here to Skip to main content
Click here to Skip to main content

How to embed an image in email body

By , 20 Feb 2012
 
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("balaji.birajdar@bogusdomain.com");
        Mail.To.Add("balaji.birajdar@bogusdomain.com");
        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";
        
        //Patch for Thunderbird as suggested by Jorge Valencia in  the comment below
        LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
        //Patch ends

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString("You should see image next to this line. <img src=cid:MyPic>", null, "text/html");
 
        htmlView.LinkedResources.Add(LinkedImage);
        Mail.AlternateViews.Add(htmlView);
        SmtpClient smtp = new SmtpClient("111.111.111.111", 25); 
        try
        {
            smtp.Send(Mail);
        }
        catch (SmtpException ex)
        {
            throw ex;
        }
Let me know if this post helps you.

License

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

About the Author

aspnet_regiis -u
Web Developer
India India
Software developer by profession, working for a service and product based organisation in Pune, India.
 
Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.
 
Interests:
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.
Follow on   Twitter

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank you Jorge for the update. I will include it in my tip....memberB Birajdar19-Feb-12 19:40 
Thank you Jorge for the update. I will include it in my tip. And thanks for the +5
GeneralFive Man! Thunderbird not show any image embedded. Only a b...memberJorge Valencia17-Feb-12 10:08 
Five Man!
 
Thunderbird not show any image embedded. Only a broken document icon.
 
Exploring the content the message arrived (Ctrl + U) I see this line:
 
Content-Type: application/octet-stream
 
Of course this is a error. I patch the code whit a:
 
LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
 
and Message display fine. Thxs again.
GeneralThank you Jawed, skantg, drewman, Tushar ..Its the appreciat...memberB Birajdar15-Feb-12 22:42 
Thank you Jawed, skantg, drewman, Tushar ..Its the appreciation by the experts like you which keeps me working .. Thanks again .. Smile | :)
GeneralReason for my vote of 5 Good WorkmemberTushar_Patil15-Feb-12 0:14 
Reason for my vote of 5
Good Work
GeneralReason for my vote of 5 Nice one! This issue has had me scra...memberdrewman51509-Feb-12 1:10 
Reason for my vote of 5
Nice one! This issue has had me scratching my head for a while now.
GeneralReason for my vote of 5 a good tip, often needed.memberskantg8-Feb-12 21:30 
Reason for my vote of 5
a good tip, often needed.
GeneralReason for my vote of 4 Nice tips..i have seen loads of user...memberjawed.ace7-Feb-12 1:35 
Reason for my vote of 4
Nice tips..i have seen loads of user looking for this one..

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 20 Feb 2012
Article Copyright 2012 by aspnet_regiis -u
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid