VB.NET Tips / Tricks / Examples and Help

VB.NET Embed Images in MailMessage

In the .NET framework, we can use the System.Net.Mail.MailMessage class to create an email message and send it using the SMTPClient class. You can make emails that are plain text or HTML based, and you can also add attachments. However there didn't seem to be a simple way send an email that would have an image embedded in the body.

You can reference absolute URLs in your HTML message, by specifying the image source as "http://www.domain.com/image.jpg", but what about if you just wanted to include the image, embedded in the email itself. It turns out you can, using something called AlternateViews for the MailMessage object. Here is a quick sample to show you how it is done: First we add 2 imports at the top

Imports System.IO

Imports System.Net.Mail

The actual method of sending the MailMessage is similar to any standard MailMessage object you would send. The difference comes in by creating a LinkedResource object, which will be our image, and then then an AlternateView object. The LinkedResource needs to have a unique string ID set for it, so that when you create the HTML for the message, you can reference the embedded image.

Here is the sample code, with comments to explain what is going on. Note the HTML body uses an <img> tag with a src attribute setting of cid with the ContentID you specify with your linked resource. If you use the image more than once in the email, you only need to create one linked resource for it.

    Public Sub SendEmail()

 

        'CREATE MAIL MESSAGE

        Using myMailMessage As New MailMessage

 

            myMailMessage.To.Add("recipent@domain.com")

            myMailMessage.From = New MailAddress("me@mydomain.com")

            myMailMessage.Subject = "This is the email subject"

            myMailMessage.Body = "This is the default text body"

            myMailMessage.IsBodyHtml = True 'THIS WILL MAKE THE MESSAGE USE THE ALT HTML VIEW

 

            'CREATE ALT HTML BODY THAT WILL INCLUDE EMBEDDED IMAGE

            'NOTE THE IMG SRC IS CID:ThePictureID

            Dim myMailHTMLBody = "<html><head></head><body>This is a test and should include a picture: <img src=cid:ThePictureID></body></html>"

 

            'BYTES ARRAY OF IMAGE SO WE CAN PUT IN MEMORY STREAM

            Dim myImageData() As Byte = Nothing

 

            'GRAB IMAGE FROM FILE AND PUT IN MEMORY STREAM

            Using myImage = Image.FromFile("C:\directory\image.jpg")

                Dim IC As New ImageConverter

                myImageData = DirectCast(IC.ConvertTo(myImage, GetType(Byte())), Byte())

            End Using

 

            Using myStream As New MemoryStream(myImageData)

                'CREATE ALT VIEW

                Dim myAltView As AlternateView = AlternateView.CreateAlternateViewFromString(myMailHTMLBody, New System.Net.Mime.ContentType("text/html"))

                'CREATE LINKED RESOURCE FOR ALT VIEW

                Dim myLinkedResouce = New LinkedResource(myStream, "image/jpeg")

                'SET CONTENTID SO HTML CAN REFERENCE CORRECTLY

                myLinkedResouce.ContentId = "ThePictureID" 'this must match in the HTML of the message body

 

                'ADD LINKED RESOURCE TO ALT VIEW, AND ADD ALT VIEW TO MESSAGE

                myAltView.LinkedResources.Add(myLinkedResouce)

                myMailMessage.AlternateViews.Add(myAltView)

 

                'SEND EMAIL

                Using mySMTP As New SmtpClient

                    mySMTP.Host = "smtp.yourdomain.com"

                    mySMTP.Credentials = New System.Net.NetworkCredential("you@domain.com", "password")

                    mySMTP.Send(myMailMessage)

                End Using

 

            End Using

        End Using

    End Sub

Hopefully using this code, you will be able to send out your emails that include images embedded in them, instead of having to link to them externally.


Posted Sep 11 2014, 09:43 AM by Matthew Kleinwaks


© 2014 - ZerosAndTheOne.com - Hosting by Orcsweb (http://www.orcsweb.com/)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems