Attachment Constructors

Definition

Initializes a new instance of the Attachment class.

Overloads

Attachment(String)

Initializes a new instance of the Attachment class with the specified content string.

Attachment(Stream, ContentType)

Initializes a new instance of the Attachment class with the specified stream and content type.

Attachment(Stream, String)

Initializes a new instance of the Attachment class with the specified stream and name.

Attachment(String, ContentType)

Initializes a new instance of the Attachment class with the specified content string and ContentType.

Attachment(String, String)

Initializes a new instance of the Attachment class with the specified content string and MIME type information.

Attachment(Stream, String, String)

Initializes a new instance of the Attachment class with the specified stream, name, and MIME type information.

Attachment(String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified content string.

public Attachment (string fileName);

Parameters

fileName
String

A String that contains a file path to use to create this attachment.

Exceptions

fileName is null.

fileName is empty.

Examples

The following code example demonstrates how to call this constructor.

public static void CreateMessageInlineAttachment2(string server, string
textMessage)
{
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "jane@contoso.com",
       "ben@contoso.com",
       "A text message for you.",
       "Message: ");

    // Attach the message string to this email message.
    Attachment data = new Attachment(textMessage);
    // Send textMessage as part of the email body.
    message.Attachments.Add(data);
    ContentType content = data.ContentType;
    content.MediaType = MediaTypeNames.Text.Plain;
    //Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment2: {0}",
            ex.ToString());
    }
    data.Dispose();
}

Remarks

The properties are set as follows:

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Attachment(Stream, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified stream and content type.

public Attachment (System.IO.Stream contentStream, System.Net.Mime.ContentType contentType);

Parameters

contentStream
Stream

A readable Stream that contains the content for this attachment.

contentType
ContentType

A ContentType that describes the data in contentStream.

Exceptions

contentType is null.

-or-

contentStream is null.

Examples

The following code example demonstrates how to call this constructor.

// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendErrorLog(string server, string recipientList)
{
    // Create a message from logMailer@contoso.com to recipientList.
    MailMessage message = new MailMessage(
       "logMailer@contoso.com", recipientList);

    message.Subject = "Error Log report";
    string fileName = "log.txt";
    // Get the file stream for the error log.
    // Requires the System.IO namespace.
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    StreamReader s = new StreamReader(fs);
    int errors = 0;
    while (s.ReadLine() != null)
    {
        // Process each line from the log file here.
        errors++;
    }
    // The email message summarizes the data found in the log.
    message.Body = String.Format("{0} errors in log as of {1}",
        errors, DateTime.Now);
    // Close the stream reader. This also closes the file.
    s.Close();
    // Re-open the file at the beginning to make the attachment.
    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    // Make a contentType indicating that the log data
    // that is attached is plain text.
    ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
    // Attach the log file stream to the email message.
    Attachment data = new Attachment(fs, ct);
    ContentDisposition disposition = data.ContentDisposition;
    // Suggest a file name for the attachment.
    disposition.FileName = "log" + DateTime.Now.ToString() + ".txt";
    // Add the attachment to the message.
    message.Attachments.Add(data);
    // Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

Remarks

The TransferEncoding property is set to Base64.

If the stream's CanSeek property is false, the attachment and the MailMessage that contains it are not reusable. You must supply a stream that can be searched to reuse an attachment.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Attachment(Stream, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified stream and name.

public Attachment (System.IO.Stream contentStream, string? name);
public Attachment (System.IO.Stream contentStream, string name);

Parameters

contentStream
Stream

A readable Stream that contains the content for this attachment.

name
String

A String that contains the value for the Name property of the ContentType associated with this attachment. This value can be null.

Exceptions

contentStream is null.

Examples

The following code example demonstrates how to call this constructor.

// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedErrorLog(string server, string recipientList)
{
    // Create a message from logMailer@contoso.com to recipientList.
    MailMessage message = new MailMessage(
       "logMailer@contoso.com", recipientList);

    message.Subject = "Error Log report";
    string fileName = "log.txt";
    // Get the file stream for the error log.
    // Requires the System.IO namespace.
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    StreamReader s = new StreamReader(fs);
    int errors = 0;
    while (s.ReadLine() != null)
    {
        // Process each line from the log file here.
        errors++;
    }
    // The email message summarizes the data found in the log.
    message.Body = String.Format("{0} errors in log as of {1}",
        errors, DateTime.Now);
    // Close the stream reader. This also closes the file.
    s.Close();
    // Re-open the file at the beginning to make the attachment.
    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    // Make a ContentType indicating that the log data
    // that is attached is plain text and is named.
    ContentType ct = new ContentType();
    ct.MediaType = MediaTypeNames.Text.Plain;
    ct.Name = "log" + DateTime.Now.ToString() + ".txt";
    // Create the attachment.
    Attachment data = new Attachment(fs, ct);
    // Add the attachment to the message.
    message.Attachments.Add(data);
    // Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
    return;
}

Remarks

If name is not null or equal to String.Empty (""), the ContentType for this attachment is constructed with the Name property set to name. The TransferEncoding property is set to Base64.

If the stream's CanSeek property is false, the attachment and the MailMessage that contains it are not reusable. You must supply a stream that can be searched in order to reuse an attachment.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Attachment(String, ContentType)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified content string and ContentType.

public Attachment (string fileName, System.Net.Mime.ContentType contentType);

Parameters

fileName
String

A String that contains a file path to use to create this attachment.

contentType
ContentType

A ContentType that describes the data in fileName.

Exceptions

fileName is null.

mediaType is not in the correct format.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Attachment(String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified content string and MIME type information.

public Attachment (string fileName, string? mediaType);
public Attachment (string fileName, string mediaType);

Parameters

fileName
String

A String that contains the content for this attachment.

mediaType
String

A String that contains the MIME Content-Header information for this attachment. This value can be null.

Exceptions

fileName is null.

mediaType is not in the correct format.

Examples

The following code example demonstrates how to call this constructor.

public static void CreateMessageInlineAttachment(string server, string
textMessage)
{
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "jane@contoso.com",
       "ben@contoso.com",
       "An inline text message for you.",
       "Message: ");

    // Attach the message string to this email message.
    Attachment data = new Attachment(textMessage, MediaTypeNames.Text.Plain);
    // Send textMessage as part of the email body.
    message.Attachments.Add(data);
    ContentDisposition disposition = data.ContentDisposition;
    disposition.Inline = true;
    //Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment: {0}",
            ex.ToString());
    }
    data.Dispose();
}

Remarks

If mediaType is null or equal to String.Empty (""), the MediaType property for this attachment is set to Plain. If mediaType is not null and is not a zero-length string, it is used to construct the ContentType associated with this attachment.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Attachment(Stream, String, String)

Source:
Attachment.cs
Source:
Attachment.cs
Source:
Attachment.cs

Initializes a new instance of the Attachment class with the specified stream, name, and MIME type information.

public Attachment (System.IO.Stream contentStream, string? name, string? mediaType);
public Attachment (System.IO.Stream contentStream, string name, string mediaType);

Parameters

contentStream
Stream

A readable Stream that contains the content for this attachment.

name
String

A String that contains the value for the Name property of the ContentType associated with this attachment. This value can be null.

mediaType
String

A String that contains the MIME Content-Header information for this attachment. This value can be null.

Exceptions

stream is null.

mediaType is not in the correct format.

Examples

The following code example demonstrates how to call this constructor.

// The following example sends a summary of a log file as the message
// and the log as an email attachment.
public static void SendNamedAndTypedErrorLog(string server, string recipientList)
{
    // Create a message from logMailer@contoso.com to recipientList.
    MailMessage message = new MailMessage(
       "logMailer@contoso.com", recipientList);

    message.Subject = "Error Log report";
    string fileName = "log.txt";
    // Get the file stream for the error log.
    // Requires the System.IO namespace.
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    StreamReader s = new StreamReader(fs);
    int errors = 0;
    while (s.ReadLine() != null)
    {
        // Process each line from the log file here.
        errors++;
    }
    // The email message summarizes the data found in the log.
    message.Body = String.Format("{0} errors in log as of {1}",
        errors, DateTime.Now);
    // Close the stream reader. This also closes the file.
    s.Close();
    // Re-open the file at the beginning to make the attachment.
    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    // Create a name for the log data file.
    string name = "log" + DateTime.Now.ToString() + ".txt";
    // Create the attachment, name it, and specify the MIME type.
    Attachment data = new Attachment(fs, name, MediaTypeNames.Text.Plain);
    // Add the attachment to the message.
    message.Attachments.Add(data);
    // Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in SendNamedAndTypedErrorLog: {0}",
            ex.ToString());
    }
    data.Dispose();
    // Close the log file.
    fs.Close();
}

Remarks

If mediaType is not null or equal to String.Empty (""), it is used to construct the ContentType class associated with this attachment.

If mediaType and name both contain Name information, the value specified in name is used. The TransferEncoding property is set to Base64.

If the stream's CanSeek property is false, the attachment and the MailMessage that contains it are not reusable. You must supply a stream that can be searched in order to reuse an attachment.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1