How to send HTML email with inline picture

Vadim Rapp
CERTIFIED EXPERT
Published:
When our application needs to send an email, there are two major options. One is to use Outlook, which is what most articles on the web are about - especially on Microsoft-related websites. Naturally, this assumes that Outlook is installed. Fortunately, there's more lightweight way to do it, by using .Net library System.Net.Mail. The following simple code will do the job of sending HTML email which content is stored in the file htmlpage1.htm, part of our project:

 
    Dim f As New StreamReader(My.Application.Info.DirectoryPath & "\htmlpage1.htm")
                              Dim HTML As String = f.ReadToEnd()
                              f.Close()
                      
                      ' (assuming that the file is in fact a template, at this point we probably will manipulate the text by replacing some placeholders by the current values)
                      
                              Dim email As New System.Net.Mail.MailMessage("from-address","to-address")
                              email.Subject = "experts-exchange article"
                              email.Body = HTML
                              email.IsBodyHtml = True
                      
                              Dim smtp As New SmtpClient
                              smtp.Host = "smtp-server-address"
                              smtp.Credentials = CredentialCache.DefaultNetworkCredentials 
                              smtp.Send(email)
                      

Open in new window

Things become more complicated when the HTML that we want to send includes an image. The easiest is to specify image source in HTML as a link to the external website hosting the image:

<img src=" https://www.experts-exchange.com/images/experts-exchange/experts-exchange-logo.png" />

However, this solution has the following major drawbacks:

1. it assumes that external website is accessible by the user, and that the hosted image is in place. Considering the fact that this email may be stored and looked at years later, this is not a solid assumption. not-found2. Modern email clients will block external image by default, for security reasons. The user more likely than not will see a placeholder, possibly with the option to download the image, which he more likely than not will not do. blocked-by-client3. External content will increase the chance of the email to be blocked by antispam software. detected as spam
Much better is to "host" the image within the email itself. This can be achieved by the following steps:

1. attach the image file as regular attachment to the email
2. for the attachment, specify content-id - can be any string
3. in the HTML, specify image source as cid:<content-id> where <content-id> is the string from step #2.

Accordingly, if we want the above email to have the image sourced from logo.jpg embedded in the body, the code will become:

 
    Dim f As New StreamReader(My.Application.Info.DirectoryPath & "\htmlpage1.htm")
                            Dim HTML As String = f.ReadToEnd()
                            f.Close()
                      
                              Dim email As New System.Net.Mail.MailMessage("from-address","to-address")
                              email.Subject = "test"
                              email.Body = HTML
                              email.IsBodyHtml = True
                      
                              email.Attachments.Add(New Attachment(My.Application.Info.DirectoryPath & "\logo.jpg"))
                              email.Attachments(0).ContentId = "logo_jpg"
                      
                              Dim smtp As New SmtpClient
                              smtp.Host = "smtp-server-address"
                              smtp.Credentials = CredentialCache.DefaultNetworkCredentials
                              smtp.Send(email)
                      

Open in new window


while in the HTML the image will appear as

        <img src="cid:logo_jpg" />

As the final touch, let's not forget to double the content of our HTML email in plain text format, for the mail clients not capable of showing HTML - by using AlternatePart.

        Dim PlainPart As String = "plain text representation"
                              email.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(PlainPart, New System.Net.Mime.ContentType("text/plain")))
                      

Open in new window

0
5,272 Views
Vadim Rapp
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.