Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Net.Mail

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
LinkedResource Class

Note: This class is new in the .NET Framework version 2.0.

Represents an embedded external resource in an email attachment, such as an image in an HTML attachment.

Namespace: System.Net.Mail
Assembly: System (in system.dll)

Visual Basic (Declaration)
Public Class LinkedResource
    Inherits AttachmentBase
Visual Basic (Usage)
Dim instance As LinkedResource
C#
public class LinkedResource : AttachmentBase
C++
public ref class LinkedResource : public AttachmentBase
J#
public class LinkedResource extends AttachmentBase
JScript
public class LinkedResource extends AttachmentBase
System.Object
   System.Net.Mail.AttachmentBase
    System.Net.Mail.LinkedResource
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Embedding an image      mikepope ... Wim Veugelers   |   Edit   |   Show History

It's not always clear how to embed (as opposed to attach) an image. The following works for me. The steps are:

  • Create an HTML-formatted message.
  • Use an <img> tag in the message body.
  • For the src attribute of the <img>, point not to a URL, but to a content ID (cid). This points to the portion of the message containing the image stream.
  • Create an alternative view.
  • Create a linkedResource that slurps up the image you want to embed.
  • Assign a content ID to the linked resource -- this should match the cid you used in the <img> tag.
  • Assign the image's file name to the linked resource.

Here's some code:

Imports System.Net.Mail
Imports System.Net.Mime
Imports System.IO
  
[...]
  
Dim fromAddress As String = "mike@example.com"
Dim toAddress As String = "mike@example.com"
Dim subject As String = "Test EmbeddedImage"
Dim contentId As String = "image1"
Dim path As String = Server.MapPath("~") & "\"
Dim filename As String = path & "MyPicture.jpg"
Dim body As String = "Here is a linked resource: <img src=""cid:image1""/>"
  
Dim mailMessage As New MailMessage(fromAddress, toAddress)
mailMessage.Subject = "Testing embedded image"
Dim av1 As AlternateView
av1 = AlternateView.CreateAlternateViewFromString(body, Nothing, _
MediaTypeNames.Text.Html)
Dim linkedResource As LinkedResource = New LinkedResource(filename)
linkedResource.ContentId = contentId
linkedResource.ContentType.Name = filename
  
av1.LinkedResources.Add(linkedResource)
mailMessage.AlternateViews.Add(av1)
mailMessage.IsBodyHtml = True
Dim mailSender As New SmtpClient("smtpHost")
Try
mailSender.Send(mailMessage)
labelStatus.Text = "Message sent!"
Catch ex As Exception
labelStatus.Text = ex.Message
End Try

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker