System.Net.Mail FAQ

Complete FAQ Listing


4.4 How do I embed images in an email? Printer Friendly

Embedding images is something that is new with System.Net.Mail. To embed an image you will need to
  1. Create a LinkedResource object. The LinkedResource will actually contain the binary data of the Image. This binary data is encoded as part of the email, and sent along as part of the MailMessage.

  2. Give the LinkedResource a unique name, also known as a Content-Id.

  3. Create a HTML AlternateView.

  4. Inside that HTML text, you need to use the standard <img> tag.

  5. For the "src" value, you need to point it at the Content-Id of the LinkedResource image. This is done by using the syntax <img src="cid:whatever"> The "src=cid:" part is required for the email client to recognize the <img> tag as an embedded image, while the "whatever" part is the actual Content-Id of the LinkedResource image.  This will instruct the mail client to find an embedded image named "whatever" and display the contents *without* making a http:// request.

That's all there is to create a linked image. Below is a short but complete example that demonstrates creating an embedded image.

[ C# ]

static void EmbedImages()
{
//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";

//first we create the Plain Text part
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", null, "text/plain");

//then we create the Html part
//to embed images, we need to use the prefix 'cid' in the img src value
//the cid value will map to the Content-Id of a Linked resource.
//thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", null, "text/html");

//create the LinkedResource (embedded image)
LinkedResource logo = new LinkedResource( "c:\\temp\\logo.gif" );
logo.ContentId = "companylogo";
//add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo);

//add the views
mail.AlternateViews.Add(plainView);
mail.AlternateViews.Add(htmlView);


//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1"); //specify the mail server address
smtp.Send(mail);
}


[ VB.NET ]
Sub EmbedImages()
'create the mail message
Dim mail As New MailMessage()

'set the addresses
mail.From = New MailAddress("me@mycompany.com")
mail.To.Add("you@yourcompany.com")

'set the content
mail.Subject = "This is an email"

'first we create the Plain Text part
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("This is my plain text content, viewable by those clients that don't support html", Nothing, "text/plain")

'then we create the Html part
'to embed images, we need to use the prefix 'cid' in the img src value
'the cid value will map to the Content-Id of a Linked resource.
'thus <img src='cid:companylogo'> will map to a LinkedResource with a ContentId of 'companylogo'
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:companylogo>", Nothing, "text/html")

'create the LinkedResource (embedded image)
Dim logo As New LinkedResource("c:\temp\logo.gif")
logo.ContentId = "companylogo"
'add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo)

'add the views
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)


'send the message
Dim smtp As New SmtpClient("127.0.0.1") 'specify the mail server address
smtp.Send(mail)
End Sub 'EmbedImages