odonovanpm
asked on
Embeedding Pictures in Email (VB Application)
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.
<IMG alt="" hspace=0 src="cid:45524925" align=baseline border=0>
What is the cid reference? Thanks.
https://www.experts-exchange.com/questions/21015020/How-to-insert-a-link-which-points-to-attached-files-in-a-mail.html
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/////ZRa CgoAAAACH+ PUNvcHlyaW dodCAoQykg MTk5
NSBJRVRGLiBVbmF1dGhvcml6ZW QgZHVwbGlj YXRpb24gcH JvaGliaXRl ZC4A
etc...
--boundary-example-1--
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
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
--boundary-example-1
Content-ID: <foo4*foo1@bar.net>
Content-Type: IMAGE/GIF
Content-Transfer-Encoding:
R0lGODlhGAGgAPEAAP/////ZRa
NSBJRVRGLiBVbmF1dGhvcml6ZW
etc...
--boundary-example-1--
hence
ASKER
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.
I can apply the logic to my VB Application afterwards. Thanks again.
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.
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.
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
R0lGODlhFAAYAPcAAAAAAIAAAA CAAICAAAAA gIAAgACAgI CAgMDAwP8A AAD/AP//AA AA//8A/wD/
/////wAAAAAAAAAAAAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA AAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAMwAA ZgAAmQAAzA AA/wAzAAAz MwAzZgAzmQ AzzAAz/wBm
AABmMwBmZgBmmQBmzABm/wCZAA CZMwCZZgCZ mQCZzACZ/w DMAADMMwDM ZgDMmQDMzA DM/wD/AAD/
MwD/ZgD/mQD/zAD//zMAADMAMz MAZjMAmTMA zDMA/zMzAD MzMzMzZjMz mTMzzDMz/z NmADNmMzNm
ZjNmmTNmzDNm/zOZADOZMzOZZj OZmTOZzDOZ /zPMADPMMz PMZjPMmTPM zDPM/zP/AD P/MzP/ZjP/
mTP/zDP//2YAAGYAM2YAZmYAmW YAzGYA/2Yz AGYzM2YzZm YzmWYzzGYz /2ZmAGZmM2 ZmZmZmmWZm
zGZm/2aZAGaZM2aZZmaZmWaZzG aZ/2bMAGbM M2bMZmbMmW bMzGbM/2b/ AGb/M2b/Zm b/mWb/zGb/
/5kAAJkAM5kAZpkAmZkAzJkA/5 kzAJkzM5kz ZpkzmZkzzJ kz/5lmAJlm M5lmZplmmZ lmzJlm/5mZ
AJmZM5mZZpmZmZmZzJmZ/5nMAJ nMM5nMZpnM mZnMzJnM/5 n/AJn/M5n/ Zpn/mZn/zJ n//8wAAMwA
M8wAZswAmcwAzMwA/8wzAMwzM8 wzZswzmcwz zMwz/8xmAM xmM8xmZsxm mcxmzMxm/8 yZAMyZM8yZ
ZsyZmcyZzMyZ/8zMAMzMM8zMZs zMmczMzMzM /8z/AMz/M8 z/Zsz/mcz/ zMz///8AAP 8AM/8AZv8A
mf8AzP8A//8zAP8zM/8zZv8zmf 8zzP8z//9m AP9mM/9mZv 9mmf9mzP9m //+ZAP+ZM/ +ZZv+Zmf+Z
zP+Z///MAP/MM//MZv/Mmf/MzP /M////AP// M///Zv//mf //zP///yH5 BAEAABAALA AAAAAUABgA
AAhIAP8JHEiwoMGDCBMqVIgCRc GGCyESlJiQ okCLBzFiNK jRIUOPAzcu HEmypMmTKC eOBBlyZUaG
CFlyrBgz4kyXLUs2FJmyZ8GAAD s=
--boundary-example-1--
To: odonovanpm
From: Richie
Subject: Smiley gif in MIME HTML version 2
Mime-Version: 1.0
Content-Type: Multipart/related; boundary="boundary-example
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:
R0lGODlhFAAYAPcAAAAAAIAAAA
/////wAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA
AABmMwBmZgBmmQBmzABm/wCZAA
MwD/ZgD/mQD/zAD//zMAADMAMz
ZjNmmTNmzDNm/zOZADOZMzOZZj
mTP/zDP//2YAAGYAM2YAZmYAmW
zGZm/2aZAGaZM2aZZmaZmWaZzG
/5kAAJkAM5kAZpkAmZkAzJkA/5
AJmZM5mZZpmZmZmZzJmZ/5nMAJ
M8wAZswAmcwAzMwA/8wzAMwzM8
ZsyZmcyZzMyZ/8zMAMzMM8zMZs
mf8AzP8A//8zAP8zM/8zZv8zmf
zP+Z///MAP/MM//MZv/Mmf/MzP
AAhIAP8JHEiwoMGDCBMqVIgCRc
CFlyrBgz4kyXLUs2FJmyZ8GAAD
--boundary-example-1--
There's a free VB base64 library here: http://www.thevbzone.com/cBASE64.cls