Embed images in email with vb.net

Good afternoon all.

Join me on Facebook

I was going to wait until Monday to post this but I was pretty pumped about this today. I was asked to build email capability into an application we are building. I used the DSOFramer control to allow users to create an email message that uses MS Word. When the user completed the task the document was saved as a web page, then I read the HTML string from that file and assigned it to the body of the email message. Sounds simple enough right? Well it got more complicated when they asked to be allowed to insert images into the body of the message and have them appear in the recipients email just as it does in Outlook for them. Everywhere I go on the Internet searching on this topic said it could not be done. Well yes it can…. :). Yes, its 1.1 of the Framework but a lot of us are forced to work under that yet. .NET Framework 2.0 and above has the ability to embed images but it wasn’t helpful for this particular app. This will still work under 2.0 (the concepts) but it has one advantage I don’t have – a way to show a Plain Text version of the email. I am working on that under this code. Here with my employer it was not that important as everyone is set up to use HTML as their mail format.

What I will demonstrate here is the solution I used to get to the point where images would carry through for the email. On a future day if people are interested in this, I will post the actual code.

First, after saving the email as HTML we must remove the reference in the HTML to editdata.mso. Why? The longer the user keeps Outlook open this file will grow in size. Because it is called in HTML, when you send the email it will indicate that any user who replies to the email it will attempt to attach their editdata.mso file to the email message. What I found out was if you do not remove it when the recipient replies to a message sent out from your software, it will attempt to attach their editdata.mso file to the email. Even though you may not see it, it is there. The recipient can get around this by changing the message to plain text but most don’t know to do this. So when using the streamreader to read the document, I replace the reference to editdata.mso to blank.

Next, it is necessary to go into the temporary folder created to hold the web page and images and change the filelist.xml file to reflect the path of the email itself not to the local directory on the machine. Once again, I read the stream from the filelist.xml file into a string and replace the local path with nothing leaving just the reference to filelist.xml in that document.

I close that document. Now I need to go back to the HTML file. I must update the references in the HTML document away from the local hard drive and to the email itself. I do that and close that file.

Now I must attach the files in the temporary folder to the email. I loop through that directory, attaching each file.

Finally I send the email.

After the email is sent, I want to verify that it is in fact looking at the email itself and not anywhere on my hard drive. So I delete the HTML file, then the temporary folder.

I click send/receive and there it is. Images are in my document. When I click forward or reply it does not require those images again which is nice.

Anyway, hope everyone is having a great weekend!

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Windows Live Tags: vb.net,csharp,Microsoft,email,Embed,images,DSOFramer,message,Word,user,HTML,Outlook,Internet,.NET Framework,Plain,Text,version,code,reference,recipient,folder,path,directory,machine,files,recipients,concepts,references,editdata.mso,filelist

  1. #1 by Jennifer on October 13, 2008 - 8:47 am

    Well done!  You are right, most things are easier said than done.  This will be handy to reference.

  2. #2 by Kelly on November 7, 2008 - 7:19 am

    Thank You! I am surprised no one ever wanted the code posted. But I am thinking they are getting by with the classes made available in 2.0. I still use this method as it is less cumbersome to me than the new method.

  3. #3 by Dave on October 12, 2009 - 9:13 am

    This sounds like it might help me out too, although my source for the e-mail body is a stream Rendered by SSRS. This can obviously be sent to a file, but if I could send the Stream directly to the Body, even better. I would sure like the code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: