Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Send Inline Image Email (VB.NET 2.0)

4.19/5 (9 votes)
21 Aug 2007CPOL1 min read 1   2.2K  
Send HTML Email with embedded image files instead of external link. Most email client will display inline image directly since there is less security risk. No more broken images.

Introduction

This solution is to demonstrate how to send HTML with an embedded image file. This solution should extend to include any non-text object.

Background

I receive a lot of email from friends with broken links or images. This is because many email clients or Web mail providers block external images (i.e. referencing images hosted on other domain / servers). I feel it is annoying since it requires me to manually adjust the spam filter or add the sender as safe.

Using the Code

Download the zip file and open the solution. The code is easy to read and understand.

Please note that all fields are required. You can add your own code for validation or preset value.

VB.NET
'
' The main trick is to use ContentId to reference the attachment
'
        Dim A As System.Net.Mail.Attachment = _
            New System.Net.Mail.Attachment(txtImagePath.Text)
        Dim RGen As Random = New Random()
        A.ContentId = RGen.Next(100000, 9999999).ToString()
        EM.Body = "<img src='cid:" + A.ContentId +"'>" 

Points of Interest

Please be considerate to the recipients, if your image is very large (in terms of file size), it is still better to send HTML using external images. Don't blow up their mailbox! Otherwise, you will be added to their 'spam sender' list.

This is my first contribution to The Code Project. I tried to search for related topics but failed. Hope it is helpful to you.

History

  • 22nd August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)