Attachment Constructor (Stream, String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.Net.Mail
Assembly:  System (in System.dll)

public Attachment(
	Stream contentStream,
	string name,
	string mediaType


Type: System.IO.Stream

A readable Stream that contains the content for this attachment.

Type: System.String

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

Type: System.String

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

Exception Condition

stream is null.


mediaType is not in the correct format.

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.

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 e-mail attachment.
public static void SendNamedAndTypedErrorLog(string server, string recipientList)
	// Create a message from to recipientList.
	MailMessage message = new MailMessage(
	   "", 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.
	// The e-mail 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.
	// 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.
	// Send the message.
	// Include credentials if the server requires them.
	SmtpClient client = new SmtpClient(server);
	client.Credentials = CredentialCache.DefaultNetworkCredentials;

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

.NET Framework
Available since 2.0
Return to top