Class AlternateView
Contents
[
Hide
]AlternateView class
Represents an alternative format view of an email message, such as plain text or HTML versions. This class allows you to provide multiple content representations for email clients with different capabilities. Alternate views can include linked resources (embedded images, files) and support content encoding and transfer encoding configuration.
public class AlternateView : AttachmentBase
Constructors
| Name | Description |
|---|---|
| AlternateView(Stream) | Initializes a new instance of the AlternateView class. |
| AlternateView(string) | Initializes a new instance of the AlternateView class. |
| AlternateView(Stream, ContentType) | Initializes a new instance of the AlternateView class. |
| AlternateView(Stream, string) | Initializes a new instance of the AlternateView class. |
| AlternateView(string, ContentType) | Initializes a new instance of the AlternateView class. |
| AlternateView(string, string) | Initializes a new instance of the AlternateView class. |
Properties
| Name | Description |
|---|---|
| BaseUri { get; set; } | Gets or sets the base URI. |
| ContentId { get; set; } | Gets or sets the content id. |
| ContentStream { get; set; } | Gets or sets the content stream. |
| ContentType { get; set; } | Gets or sets the type of the content. |
| virtual Headers { get; } | Gets headers collection of attachment. |
| LinkedResources { get; } | Gets the set of embedded resources referred to by this alternate view. |
| TransferEncoding { get; set; } | Gets or sets the transfer encoding. |
| UniqueId { get; } | Gets or sets a unique identifier for the attachment that will be constant for each application run. |
Methods
| Name | Description |
|---|---|
| static CreateAlternateViewFromString(string) | Creates a AlternateView of using the content specified in a string. |
| static CreateAlternateViewFromString(string, ContentType) | Creates a AlternateView of using the content specified in a string. |
| static CreateAlternateViewFromString(string, ContentType, TransferEncoding) | Creates a AlternateView of using the content specified in a string. |
| static CreateAlternateViewFromString(string, Encoding, string) | Creates a AlternateView of using the content specified in a string. |
| Dispose() | Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| virtual Save(Stream) | Saves the specified stream. |
| virtual Save(string) | Saves the specified file name. |
Remarks
Use CreateAlternateViewFromString to create alternate views from string content. Add alternate views to AlternateViews to include them in the message.
Examples
The following example shows how to create and add AlternateView to MailMessage.
[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