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

Send Inline Image Email (VB.NET 2.0)

By , 21 Aug 2007
 
Prize winner in Competition "Best VB.NET article of Jul 2007"

Introduction

This solution is to demonstrate how to send HTML with an embedded image file. This solution should extend to include any non-text object.

Background

I receive a lot of email from friends with broken links or images. This is because many email clients or Web mail providers block external images (i.e. referencing images hosted on other domain / servers). I feel it is annoying since it requires me to manually adjust the spam filter or add the sender as safe.

Using the Code

Download the zip file and open the solution. The code is easy to read and understand.

Please note that all fields are required. You can add your own code for validation or preset value.

'
' The main trick is to use ContentId to reference the attachment
'
        Dim A As System.Net.Mail.Attachment = _
            New System.Net.Mail.Attachment(txtImagePath.Text)
        Dim RGen As Random = New Random()
        A.ContentId = RGen.Next(100000, 9999999).ToString()
        EM.Body = "<img src='cid:" + A.ContentId +"'>" 

Points of Interest

Please be considerate to the recipients, if your image is very large (in terms of file size), it is still better to send HTML using external images. Don't blow up their mailbox! Otherwise, you will be added to their 'spam sender' list.

This is my first contribution to The Code Project. I tried to search for related topics but failed. Hope it is helpful to you.

History

  • 22nd August, 2007: Initial post

License

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

About the Author

WPKF
Web Developer
Hong Kong Hong Kong
Member
A .NET Programmer for 8 years, who wants to share the experience.

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   
GeneralWorks well .NET Framework 4memberrobnick15 Jun '12 - 18:37 
Am using that code in a Windows Service - nice.
 
Thanks. Rob
General.dat file as attachmentmemberdssohal17 Jul '09 - 3:53 
Hi There,
 
I have also implement this approach in code to send inline images in body.But i have a problem if html or body contain multiple images. Then it send .dat file in attachment with the mail.
 
Any help to avoid .dat file or hide such file will be appreciated.
 
Thanks
Dalbir Singh
GeneralRe: .dat file as attachmentmemberWPKF7 Jul '09 - 5:32 
As far as I know, it is not possible since HTML and images worked together as separate file. This is still true even in email.
GeneralMultiple imagesmemberraquidd225 May '09 - 20:12 
hello
 
your code work perfect
i nedd use multiple images how use the part
Dim RGen As Random = New Random()
A.ContentId = RGen.Next(100000, 9999999).ToString()
 
thanks for the time
GeneralAwesome!memberWillem_6365 Aug '08 - 23:08 
You rock man, thanks for sharing.
GeneralThank you!memberrobinnexus4 May '08 - 21:45 
Hello WPKF,
 
Just wanted to say Thank you!
Was quick and time saving!
 
regards
Ranjit
 
.NET crazy

GeneralPerfectmemberPhilM994 Apr '08 - 16:08 
I just extracted your sample and it worked first time. thanks
GeneralHere's someone else's solutionmemberwk63328 Aug '07 - 8:13 
Looks like pretty much the same thing- but maybe it will help people having problems.
 
http://mikepope.com/blog/DisplayBlog.aspx?permalink=1264
GeneralRe: Here's someone else's solutionmemberpatelhirenn15 Jun '08 - 21:01 
Hi,
 
Can i get the same code for VB 6.0.
 
If yes then please email it to thetaurianking@yahoo.com
 
Thank You & Regds,
Hiren.
GeneralYes but...memberMaxHacker27 Aug '07 - 11:12 
Don't even bother trying to get this to work with Lotus Notes Poke tongue | ;-P

Generaltest resultsmemberOhioStev23 Aug '07 - 1:10 
Yes I see an "x" where the image should appear. The image is attached to message, NOT embedded in the message.
GeneralI attempted to use your codememberOhioStev22 Aug '07 - 10:21 
I am working on this EXACT problem using asp.net. Like you I am using version 2.0 of the framework. It is a weird coincidence that this solution was posted today. I tried to adapt your source code as follows...
 
I put this at the top of an asp.net webform:
Imports System.Net.Mail
Imports System.IO
 
I put this in the Page_Load event:
 
Dim fromStr As String = "smith@acme.com"
Dim toStr As String = "jones@yahoo.com"
Dim imgPath As String = "C:\image1.jpg")
Dim subjStr As String = "image test"
Dim bodyStr As String = "This is text."
Dim smtp As String = "[acme email server IP]"
 
Dim EM As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage(fromStr, toStr)
Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(imgPath)
Dim RGen As Random = New Random()
A.ContentId = RGen.Next(100000, 9999999).ToString()
EM.Attachments.Add(A)
EM.Subject = subjStr
EM.Body = "<body>" + bodyStr + "
<img src='cid:" + A.ContentId + "'></body>"
EM.IsBodyHtml = True
Dim SC As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(smtp)
SC.UseDefaultCredentials = True
SC.Send(EM)
 
Unfortunately it did not work for me. When I loaded the page it DID send an email to my yahoo account. However, Yahoo treated the image as an attachment.
 
Do you have any suggestions for me?
GeneralRe: I attempted to use your codememberWPKF22 Aug '07 - 17:18 
I tried with 3 common webmail (Yahoo!, Hotmail, GMail). My code works for me for all of them.
 
Do you see a broken image?

GeneralRe: I attempted to use your codememberOhioStev23 Aug '07 - 1:44 
Oops! I put my last message in the wrong place. Here is more information about my tests:
 
Tuesday- I used a function quite similar to yours to send email to my yahoo account. It worked...the embedded images appeared.
 
Wednesday- In the morning I tested my function again. Now I could not see images in my test messages! I decided that my code must have some sort of problem. I did an Internet search and found this thread.
 
Wednesday afternoon- I implemented your code. Your method behaved almost exactly like mine. The message appeared in the Yahoo account, but the images were Xed out. However, the images did appear as attachments.
 
Thursday morning (now)- I made a hotmail account and used your code to send a message. The message appeared in my hotmail account. At first, the images did NOT appear. Then I clicked on a button whose label said something like "trust this sender". After that the image DID appear. However, the image was also an attachment.
 
After all of that testing, here is my theory: I think that security settings are an issue. I suspect that I stopped seeing images in my yahoo account because yahoo became suspicious of the sender.
GeneralRe: I attempted to use your codememberOhioStev23 Aug '07 - 4:41 
This article explores how different sites handle embedded images:
 
http://www.campaignmonitor.com/blog/archives/2007/02/current_conditions_and_best_pr_1.html
 
I think that your code is good but not all email hosts have the same policies. Also, individual users may choose to block all images. So several different factors will effect whether the recipeint sees the image.
GeneralRe: I attempted to use your codememberWPKF23 Aug '07 - 15:30 
Thanks for your link and sharing of your experience.
 
I found there are 2 more article in CodeProject using C#, using "AlternateView" .NET class. You may also have a look at them. Smile | :)
GeneralNeatmemberBen Daniel21 Aug '07 - 19:43 
Wow, it's so simple. Thanks.
 
Thanks,
Ben Smile | :)

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 22 Aug 2007
Article Copyright 2007 by WPKF
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid