[How Do I:] Embed an Image in an Email with ASP.NET

Please install Silverlight or click download to watch video locally.

Chris Pels shows how to embed an image in an email with ASP.NET. He creates a web form (with fields for To, From, Subject, and Body), uses the AlternateView class to create text and HTML versions of an email, stores an image in a LinkedResource class instance, embeds it in the HTML AlternateView. He then adds both versions to the MailMessage object and sends the email twice, first with HTML-receiving capabilities enabled and then as text-only. Both email messages appear in Outlook. The HTML version shows the embedded image; the text version does not.

Presented by Chris Pels

Duration: 19 minutes, 56 seconds

Date: 06 November 2008

Watch    Video   |   Download    Video   |   VB Code    C# Code

Video downloads: WMV  |  Zune  |  iPod  |  PSP  |  MPEG-4  |  3GP

Audio downloads: AAC  |  WMA  |  MPEG-4  |  MPEG-3  |  MPEG-2

Comments : 13

Leave a Comment

sumitmundada : On November 07, 2008 5:58 AM said:

This is awesome post for inline images attached to mails

agoyal : On November 08, 2008 1:06 PM said:

This a good video has option of embeded video

uniquesaiful : On November 10, 2008 2:38 AM said:

A good topic is discussed in this video

djafer : On November 13, 2008 1:49 AM said:

Çok teşekkürler

acroitoriu : On November 13, 2008 2:40 AM said:

nice and useful video... thanks!

ramy ramadan : On December 06, 2008 7:00 AM said:

Thanks Chris Pels

kathy.yazzie : On December 17, 2008 10:49 PM said:

Thanks this was a big help. One note:  I had to put quotes around the id - src=\"cid:HDIImage\" for this to work properly with outlook.

bohunt : On February 13, 2009 5:42 PM said:

Very useful - thanks!

JustinStolle : On May 05, 2009 10:52 PM said:

You have an error in your sample code. Make sure your image tag is written using "img" instead of "image". If you don't do that, the attachment will not embed and show up as an ATT0000000.dat file.

uwspstar : On May 26, 2009 7:24 PM said:

Nice video , Great job

gretchen : On June 15, 2009 3:11 AM said:

There is a serious error - the embedded image works fine, but the alternateviews don't work at all!

Pels uses an input form for the body of the email so when he switches outlook to plain text he also gets nothing more than the html version - it fooled me because he inputed "this is a text version", but the email actually said at the top "the message was converted to plain text" and he had the ATTxxx as an attachment. He is NOT getting his text alternateview! (That is, if he hard coded "this is text" in the text view and "this is html" in the html view, he would NEVER see "this is text" no matter what the Outlook setting was)

Now my question is: is this unique to outlook 2003 and it is impossible for it to pick up the text alternateview?

How do you program alternateviews to work correctly???

mc9000 : On August 01, 2009 2:09 AM said:

This video is showing the method CreateAlternateViewFromString, but it does not show up as a method in VS2008.

Is this not for DotNet 3.5 ?

system.net.Mail.AlternateView.CreateAlternateViewFromString <-- does not exist as a method in intellisense or at all

sunandash : On October 26, 2009 10:57 PM said:

Hey Thanks a lot for the video. I have exactly the same code which i am using to send some mails through a windows app. But i have two images to embed where the second image has to change everytime.

I use two instances of the kind linkedresource in this case and later use concatenate function to get both of them. -

Dim collection As LinkedResourceCollection = htmlView.LinkedResources()

collection.Insert(0, background)

collection.Insert(1, EmpImage)

htmlView.LinkedResources.Concat(collection)

where background and EmpImage are LinkedResource instances.

Not sure if this is the right way I get an exception saying-

"System.Runtime.InteropServices.ExternalException: A generic error occured in GDI+ at System.Drawing"

Please Help.

Leave a Comment

You must be logged in to leave a comment. Click here to log in.

Microsoft Communities