Solved

Embeedding Pictures in Email (VB Application)

Posted on 2004-10-15
9
196 Views
Last Modified: 2010-04-23
I am building a small email application that will allow my clients to send bulk emails to their customers. I have used HTML emails before but each time I want a product image to appear on the page I reference a URL (http://www.elro.ie/Image.gif). I recently received an email that had an image embedded in it with no reference to an online gif or jpeg. The code appeared like this:

<IMG alt="" hspace=0   src="cid:45524925" align=baseline border=0>

What is the cid reference? Thanks.
0
Comment
Question by:odonovanpm
  • 5
  • 2
  • 2
9 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12323803
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12323840
Emails like that are in "MIME HTML" format, as described by RFC 2110 (http://www.faqs.org/rfcs/rfc2110.html).
Each image is included in the email as an attachment with a 'Content-ID: xxx' header.  The main HTML content of the email then references that ID in the 'src' attribute.  An entire MIME HTML email message might look like this (example taken from the RFC) -

From: foo1@bar.net
To: foo2@bar.net
Subject: A simple example
Mime-Version: 1.0
Content-Type: Multipart/related; boundary="boundary-example-1";
          type=Text/HTML

--boundary-example-1
Content-Type: Text/HTML; charset=US-ASCII

... text of the HTML document, which might contain a hyperlink
to the other body part, for example through a statement such as:
<IMG SRC="cid:foo4*foo1@bar.net" ALT="IETF logo">

--boundary-example-1
Content-ID: <foo4*foo1@bar.net>
Content-Type: IMAGE/GIF
Content-Transfer-Encoding: BASE64

R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNvcHlyaWdodCAoQykgMTk5
NSBJRVRGLiBVbmF1dGhvcml6ZWQgZHVwbGljYXRpb24gcHJvaGliaXRlZC4A
etc...

--boundary-example-1--
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12323941
hence
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:odonovanpm
ID: 12324025
OK, I think I understand what you are trying to show me in your example... But how can I attach an image so that it will be included in the header of the message? I am not 100% sure how I am meant to create the HTML document. If I was in a HTML editor how would I go about doing this "MIME HTML" format?

I can apply the logic to my VB Application afterwards. Thanks again.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12388995
Assuming there's no build-in VB library for building these things, you'll need to either use a third party email library (eg. http://www.chilkatsoft.com/email-dotnet.asp), or build the message yourself.

Building it yourself shouldn't be hard, but I don't know much VB so I can't show you any code.  The HTML itself is standard HTML, which you'd write in any HTML editor - the only difference is the 'src' attributes of the images.  The images need encoding using base64 - I'm sure there must be built-in support for base64 in VB, or if not there'll be a free component for it.

You can use the MSHTML DOM library to find all the images in your HTML, from there get a list of the images you need to include in the message, assign them 'cid's, and change their 'src' attributes in the HTML to contain the 'cid's rather than the filenames.  Then put together the processed HTML and the images as shown above - the boundary can be anything you like, as long as it doesn't appear anywhere in the content of the message.
0
 

Author Comment

by:odonovanpm
ID: 12389408
But how do I physically include/save the files in an email... I'm sure this is very simple but I just can't get my head around it? Could you give me HTML code showing how to include an image in an email message?
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 12390028
> But how do I physically include/save the files in an email [...] ?

The example above does exactly that.  The piece that looks like this:

   Content-Transfer-Encoding: BASE64

   R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNvcHlyaWdodCAoQykgMTk5
   NSBJRVRGLiBVbmF1dGhvcml6ZWQgZHVwbGljYXRpb24gcHJvaGliaXRlZC4A
   etc...

*is* the image.  It's encoded using base64, which is an encoding technique that takes binary data like an image and encodes it as printable characters.  Email programs know how to decode that text to retrieve the original image data.  There must be a VB library to do base64 encoding.

Here's a complete example.  Save this as 'smile.mht' and double-click it in Windows Explorer.  Internet Explorer should start, and show you an HTML page with a picture of a smiley face in it.  Forgive my meagre artwork!

To: odonovanpm
From: Richie
Subject: Smiley gif in MIME HTML
Mime-Version: 1.0
Content-Type: Multipart/related; boundary="boundary-example-1";
          type=Text/HTML

--boundary-example-1
Content-Type: Text/HTML; charset=US-ASCII

<html><body><p>Here is an image: <IM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M/9mZv9mmf9mzP9m//+ZAP+ZM/+ZZv+Zmf+Z
zP+Z///MAP/MM//MZv/Mmf/MzP/M////AP//M///Zv//mf//zP///yH5BAEAABAALAAAAAAUABgA
AAhIAP8JHEiwoMGDCBMqVIgCRcGGCyESlJiQokCLBzFiNKjRIUOPAzcuHEmypMmTKCeOBBlyZUaG
CFlyrBgz4kyXLUs2FJmyZ8GAADs=

--boundary-example-1--
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12390059
Aha!  I've done some further reading on this, and I've found a way that you can do this without editing the HTML.  When you embed the image, you give it a Content-Location header that matches the 'href' attribute of the 'img' tag in the HTML.  Here's that last example again, but this time the HTML is exactly as it might come out of your HTML editor.  Note the match between the IMG SRC and the Content-Location header:

To: odonovanpm
From: Richie
Subject: Smiley gif in MIME HTML version 2
Mime-Version: 1.0
Content-Type: Multipart/related; boundary="boundary-example-1";
          type=Text/HTML

--boundary-example-1
Content-Type: Text/HTML; charset=US-ASCII

<html><body>
<p>Here is an image: <IMG SRC="smile.gif"></p>
</body></html>

--boundary-example-1
Content-Location: smile.gif
Content-Type: IMAGE/GIF
Content-Transfer-Encoding: BASE64

R0lGODlhFAAYAPcAAAAAAIAAAACAAICAAAAAgIAAgACAgICAgMDAwP8AAAD/AP//AAAA//8A/wD/
/////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMwAAZgAAmQAAzAAA/wAzAAAzMwAzZgAzmQAzzAAz/wBm
AABmMwBmZgBmmQBmzABm/wCZAACZMwCZZgCZmQCZzACZ/wDMAADMMwDMZgDMmQDMzADM/wD/AAD/
MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA/zMzADMzMzMzZjMzmTMzzDMz/zNmADNmMzNm
ZjNmmTNmzDNm/zOZADOZMzOZZjOZmTOZzDOZ/zPMADPMMzPMZjPMmTPMzDPM/zP/ADP/MzP/ZjP/
mTP/zDP//2YAAGYAM2YAZmYAmWYAzGYA/2YzAGYzM2YzZmYzmWYzzGYz/2ZmAGZmM2ZmZmZmmWZm
zGZm/2aZAGaZM2aZZmaZmWaZzGaZ/2bMAGbMM2bMZmbMmWbMzGbM/2b/AGb/M2b/Zmb/mWb/zGb/
/5kAAJkAM5kAZpkAmZkAzJkA/5kzAJkzM5kzZpkzmZkzzJkz/5lmAJlmM5lmZplmmZlmzJlm/5mZ
AJmZM5mZZpmZmZmZzJmZ/5nMAJnMM5nMZpnMmZnMzJnM/5n/AJn/M5n/Zpn/mZn/zJn//8wAAMwA
M8wAZswAmcwAzMwA/8wzAMwzM8wzZswzmcwzzMwz/8xmAMxmM8xmZsxmmcxmzMxm/8yZAMyZM8yZ
ZsyZmcyZzMyZ/8zMAMzMM8zMZszMmczMzMzM/8z/AMz/M8z/Zsz/mcz/zMz///8AAP8AM/8AZv8A
mf8AzP8A//8zAP8zM/8zZv8zmf8zzP8z//9mAP9mM/9mZv9mmf9mzP9m//+ZAP+ZM/+ZZv+Zmf+Z
zP+Z///MAP/MM//MZv/Mmf/MzP/M////AP//M///Zv//mf//zP///yH5BAEAABAALAAAAAAUABgA
AAhIAP8JHEiwoMGDCBMqVIgCRcGGCyESlJiQokCLBzFiNKjRIUOPAzcuHEmypMmTKCeOBBlyZUaG
CFlyrBgz4kyXLUs2FJmyZ8GAADs=

--boundary-example-1--
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12390067
There's a free VB base64 library here: http://www.thevbzone.com/cBASE64.cls
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question