Reputation: 6450
I would like to insert images into an email that is in HTML form. I can see that directly embedding an image won't produce a good result in Outlook, so I have to find another way. using a server to host the image is also out of the question as I do not have a server for that. However, when I looked at the original content of an email sent via Outlook, i see something like this.
--_004_F8160BEB14FE9D41AE00F41E46A5412B109DEBCFAAdruexc02dillo_
Content-Type: image/gif; name="image001.gif"
Content-Description: image001.gif
Content-Disposition: inline; filename="image001.gif"; size=3038;
creation-date="Tue, 01 Oct 2013 14:30:35 GMT";
modification-date="Tue, 01 Oct 2013 14:30:35 GMT"
Content-ID: <[email protected]>
Content-Transfer-Encoding: base64
R0lGODlhbgA6AOfRAMQAEMUBEcUCGMYFGccJGsMWGMQYGcUaGsUaIMYcIcgeIsceKMkfI8ggKcoh
KcsjKswkK8wlMcksK8gsMMotLMotMcsuMswwM80xNM0xOc8yNc4zOs80O9E1PFZYVc06PM48Pc48...
and so on. I also see this in the HTML :
<img width=3D110 height=3D58 id=3D"Image_x0020_1" src=3D"cid:[email protected]" alt=3Dimage001>
What I understand from this is that the image is somehow inserted into the email and then refered to in the HTML using the cid proterty.
My question is this, using the .net objets MailMessage, is there a way to actually do this ? If so, how can I 'embed' image, get the cid it uses and insert into my html message ?
Thanks,
Upvotes: 1
Views: 975
Reputation: 765
You create an AlternateView and then add the images to it's LinkedResources collection, here's an example:
Dim mailMessage as New MailMessage()
Dim bodyHTML as String = (create your html email here however you like)
Dim htmlView as Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString(bodyHTML, Nothing, "text/html")
Dim pathOfImages as String = "\Images" (or wherever they are in your project)
Dim imagePath as String = Path.Combine(pathOfImages, "testimage.png")
Dim imageResource as New Mail.LinkedResource(imagePath, "image/png")
imageResource.ContentId = "testimageID" <- this is the cid: you have noticed and that you need to use to reference the image in your html email, so this one will be <img src='cid:testimageID'>
imageResource.TransferEncoding = System.Net.Mime.TransferEncoding.Base64
htmlView.LinkedResources.Add(imageResource)
mailMessage.AlternateViews.Add(htmlView)
mailMessage.IsBodyHtml = True
Upvotes: 2