Class LinkedResource
Contents
[
Hide
]LinkedResource class
Represents an embedded resource (such as an image or file) that can be referenced by an AlternateView in an email message. Linked resources are typically used to embed images, fonts, or other assets directly into HTML email content using Content-ID references.
public class LinkedResource : AttachmentBase
Constructors
| Name | Description |
|---|---|
| LinkedResource(Stream) | Initializes a new instance of the LinkedResource class with the content of a stream. |
| LinkedResource(string) | Initializes a new instance of the LinkedResource class with the content of a file. |
| LinkedResource(Stream, ContentType) | Initializes a new instance of the LinkedResource class with the content of a stream. |
| LinkedResource(Stream, string) | Initializes a new instance of the LinkedResource class with the content of a stream. |
| LinkedResource(string, ContentType) | Initializes a new instance of the LinkedResource class with the content of a file. |
| LinkedResource(string, string) | Initializes a new instance of the LinkedResource class with the content of a file. |
Properties
| Name | Description |
|---|---|
| ContentDisposition { get; } | Gets the Content-Disposition header of the resource. |
| ContentId { get; set; } | Gets or sets the content ID of the part, by which HTML refers to it with a cid: URL. |
| ContentLink { get; set; } | Gets or sets the URI by which the HTML refers to the resource, as an alternative to its content ID. |
| ContentStream { get; set; } | Gets or sets the stream that holds the content. |
| ContentType { get; set; } | Gets or sets the content type of the part: its media type, name and character set. |
| virtual Headers { get; } | Gets the MIME headers of the part. |
| TransferEncoding { get; set; } | Gets or sets how the content is encoded for transfer, such as Base64 or quoted-printable. |
| UniqueId { get; } | Gets an identifier of the part that stays the same while the application runs. |
Methods
| Name | Description |
|---|---|
| static CreateLinkedResourceFromString(string) | Creates a linked resource from a string. |
| static CreateLinkedResourceFromString(string, ContentType) | Creates a linked resource from a string. |
| static CreateLinkedResourceFromString(string, Encoding, string) | Creates a linked resource from a string. |
| Dispose() | Releases the resources used by the part, including its content stream. |
| virtual Save(Stream) | Saves the decoded content of the part to a stream. |
| virtual Save(string) | Saves the decoded content of the part to a file. |
Remarks
Linked resources are added to LinkedResources and referenced in HTML content using the ‘cid:’ prefix (e.g., <img src=‘cid:resourceId’>). They differ from regular Attachment objects in that they are embedded within the message body rather than appearing as downloadable attachments.
Examples
The following example shows how to embed objects using LinkedResource into an Email.
[C#]
var eml = new MailMessage
{
From = "AndrewIrwin@from.com",
To = "SusanMarc@to.com",
Subject = "This is an email"
};
// Create the plain text part It is viewable by those clients that don't support HTML
var plainView =
AlternateView.CreateAlternateViewFromString("This is my plain text content", null, "text/plain");
// 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:barcode'>
// will map to a LinkedResource with a ContentId of 'barcode'.
var htmlView =
AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", null,
"text/html");
// Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
var barcode = new LinkedResource("1.jpg", MediaTypeNames.Image.Jpeg)
{
ContentId = "barcode"
};
eml.LinkedResources.Add(barcode);
eml.AlternateViews.Add(plainView);
eml.AlternateViews.Add(htmlView);
eml.Save("EmbeddedImage_out.msg", SaveOptions.DefaultMsgUnicode);
[Visual Basic]
Dim eml = New MailMessage With {
.From = "AndrewIrwin@from.com",
.[To] = "SusanMarc@to.com",
.Subject = "This is an email"
}
' Create the plain text part It is viewable by those clients that don't support HTML
Dim plainView = AlternateView.CreateAlternateViewFromString("This is my plain text content", Nothing, "text/plain")
' 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:barcode'>
' will map to a LinkedResource with a ContentId of 'barcode'.
Dim htmlView = AlternateView.CreateAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", Nothing, "text/html")
' Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
Dim barcode = New LinkedResource("1.jpg", MediaTypeNames.Image.Jpeg) With {
.ContentId = "barcode"
}
eml.LinkedResources.Add(barcode)
eml.AlternateViews.Add(plainView)
eml.AlternateViews.Add(htmlView)
eml.Save("EmbeddedImage_out.msg", SaveOptions.DefaultMsgUnicode)
See Also
- class AttachmentBase
- namespace Aspose.Email
- assembly Aspose.Email