Send email in .NET with embedded image

There are many example available on internet to send email using System.Net.Mail. But very few contains code to include image in the body of the email.
System.Net.Mail contains a class LinkedResource which is used to embedded external resource in an email attachment, such as an image in an HTML attachment. We can include as many images as we want using this class and can design the html code accordingly to display in the email body. LinkedResource contains a property ContentId which is MIME Content ID of the image which will allows to identify the image in the body of the image.




Dim imageResource1 As New LinkedResource("C:\MyPhotos\Photo1.jpg", "image/jpeg")
imageResource1.ContentId = "uniqueId1"
imageResource1.TransferEncoding = TransferEncoding.Base64



Now if we want to add this image in body of email, we have set "cid:uniqueID1" in src tag of the image.


Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody.ToString, Nothing, "text/html")
'adding the imaged linked to htmlView...
htmlView.LinkedResources.Add(imageResource1)
mail.AlternateViews.Add(htmlView)


Now after creating content of html we have to create an AlternateView and add htmlText for the email as well as add images in LinkedResources collection of HTMLView

Full code is attached for this snipplet


Attachments

  • Code to Send email with embedded Image (34665-9655-Email.vb.txt)
  • Comments

    Author: krishnavenikaladi10 Nov 2009 Member Level: Gold   Points : 0

    hi,
    good snippet.
    east to understand..keep contributing



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: