AttachmentBase Class
Namespace: System.Net.Mail
Assembly: System.Net.Mail.dll
Represents the base class for attachments that can be added to an e-mail message.
This class is abstract and cannot be instantiated directly. Use specific classes
like Attachment
or LinkedResource
.
Inheritance
System.Object
System.MarshalByRefObject
System.Net.Mail.AttachmentBase
Syntax
public abstract class AttachmentBase
Properties
The AttachmentBase
class exposes the following properties:
ContentDisposition
System.Net.Mime.ContentDisposition
Gets the content disposition for the attachment.
ContentId
string
Gets or sets the content ID for the attachment. This is used for inline attachments (e.g., images in HTML content).
ContentType
System.Net.Mime.ContentType
Gets the content type of the attachment.
Name
string
Gets or sets the name of the attachment. This is the filename that will be displayed to the recipient.
TransferEncoding
System.Net.Mail.TransferEncoding
Gets or sets the transfer encoding for the attachment.
Methods
The AttachmentBase
class does not define any public methods directly. Its derived classes may implement specific functionalities.
Remarks
The AttachmentBase
class provides common functionality for attachments used in email messages.
It defines properties for content disposition, content ID, content type, name, and transfer encoding.
The actual attachment content is managed by derived classes such as Attachment
(for files)
and LinkedResource
(for inline content like images within HTML).
When sending an email with attachments using SmtpClient
, you add instances of derived classes
to the AttachmentsCollection
of a MailMessage
object.
Example
The following example demonstrates how to create a MailMessage
with a file attachment.
Note that AttachmentBase
is abstract, so we use the concrete Attachment
class.
using System.Net.Mail;
public class Mailer {
public static void SendEmailWithAttachment() {
// Create a new MailMessage object
using (MailMessage message = new MailMessage()) {
message.From = new MailAddress("sender@example.com");
message.To.Add("recipient@example.com");
message.Subject = "Email with Attachment";
message.Body = "Please find the attached document.";
// Create an Attachment object from a file
string filePath = "path/to/your/document.pdf";
Attachment attachment = new Attachment(filePath, System.Net.Mime.MediaTypeNames.Application.Pdf);
// Add the attachment to the message
message.Attachments.Add(attachment);
// Configure the SmtpClient and send the email
using (SmtpClient smtpClient = new SmtpClient("smtp.example.com")) {
smtpClient.Credentials = new System.Net.NetworkCredential("sender@example.com", "your_password");
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Send(message);
Console.WriteLine("Email sent successfully with attachment!");
}
}
}
}