Class Attachment
Contents
[
Hide
]Attachment class
Represents an email attachment that can be added to a MailMessage. This class provides functionality for managing attachment content, name, content type, encoding, and content disposition. It supports various attachment types including regular files, embedded messages (message/rfc822), and TNEF formatted messages.
public class Attachment : AttachmentBase, IAttachment, IPreferredTextEncodingProvider
Constructors
| Name | Description |
|---|---|
| Attachment(string) | Initializes a new instance of the Attachment class with the content of a file. |
| Attachment(Stream, ContentType) | Initializes a new instance of the Attachment class with the content of a stream. |
| Attachment(Stream, string) | Initializes a new instance of the Attachment class with the content of a stream. |
| Attachment(string, ContentType) | Initializes a new instance of the Attachment class with the content of a file. |
| Attachment(string, string) | Initializes a new instance of the Attachment class with the content of a file. |
| Attachment(Stream, string, string) | Initializes a new instance of the Attachment class with the content of a stream. |
Properties
| Name | Description |
|---|---|
| ContentDisposition { get; } | Gets the Content-Disposition header of the attachment, which tells whether it is shown inline or as a file, and holds its file name and dates. |
| ContentId { get; set; } | Gets or sets the content ID of the part, by which HTML refers to it with a cid: URL. |
| 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. |
| IsEmbeddedMessage { get; } | Gets a value indicating whether the attachment is an attached e-mail message (message/rfc822). |
| IsTnef { get; } | Gets a value indicating whether the attachment is a TNEF-encoded message (winmail.dat). |
| IsUri { get; } | Gets a value indicating whether the attachment is a link to content elsewhere rather than the content itself. |
| Name { get; set; } | Gets or sets the file name of the attachment. |
| NameEncoding { get; set; } | Gets or sets the encoding used for the file name of the attachment in the headers. |
| PreferredTextEncoding { get; set; } | Gets or sets the encoding used for the text of the attachment when no other encoding is specified. |
| 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 CreateAttachmentFromString(string, ContentType) | Creates an attachment from a string with the specified content type. |
| static CreateAttachmentFromString(string, string) | Creates a text attachment from a string. |
| static CreateAttachmentFromString(string, string, Encoding, string) | Creates an attachment from a string with the specified encoding and media type. |
| 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
Use CreateAttachmentFromString to create attachments from string content. Add attachments to Attachments to include them in the message.
Examples
The following example shows how to add and remove an Attachment from MailMessage.
[C#]
// Create an instance of MailMessage class
var eml = new MailMessage {From = "sender@sender.com", To = "receiver@gmail.com"};
// Load an attachment
var attachment = new Attachment("1.txt");
eml.Attachments.Add(attachment);
// Remove attachment from your MailMessage
eml.Attachments.Remove(attachment);
[Visual Basic]
' Create an instance of MailMessage class
Dim eml = New MailMessage With {
.From = "sender@sender.com",
.[To] = "receiver@gmail.com"
}
' Load an attachment
Dim attachment = New Attachment("1.txt")
eml.Attachments.Add(attachment)
' Remove attachment from your MailMessage
eml.Attachments.Remove(attachment)
See Also
- class AttachmentBase
- interface IAttachment
- interface IPreferredTextEncodingProvider
- namespace Aspose.Email
- assembly Aspose.Email