Sunday, July 6, 2008

Embed image in HTML message in .NET Framework

Adding an image to an email message in .NET Framework is quite simple by using the attachment features. However doing so, the receiver of the email will have to explicitly open the image (or download it first) when opening the message.

An alternative way would be to use an HTML-formatted message and embed the image in the message. In this way, as the receiver opens the message, the image will appear automatically.

How is it achieved in .NET Framework? Very simple.

1. Create an email message, setting the from address, to address and subject text accordingly.
2. Create an html body, and use the img tag to embed the image. Set the content id (cid) in the src attribute correctly.
3. Create an alternative view for the html message.
4. Create linked resource object for the image to be embedded. Set the content id.
5. Add the newly created linked resource object to the html alternative view.
6. Add the alternative view to the email message.
7. Set the IsBodyHtml property to true.

8. Send the email message using SmtpClient (not in this post).

1 comment:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!