Skip to main content
.NET Framework Class Library
Attachment Constructor (Stream, ContentType)

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

Namespace: System.Net.Mail
Assembly: System (in System.dll)
Syntax
Public Sub New ( _
	contentStream As Stream, _
	contentType As ContentType _
)
public Attachment(
	Stream contentStream,
	ContentType contentType
)
public:
Attachment(
	Stream^ contentStream, 
	ContentType^ contentType
)
new : 
        contentStream:Stream * 
        contentType:ContentType -> Attachment

Parameters

contentStream
Type: System.IO..::.Stream
A readable Stream that contains the content for this attachment.
contentType
Type: System.Net.Mime..::.ContentType
A ContentType that describes the data in stream.
Exceptions
ExceptionCondition
ArgumentNullException

contentType is nullNothingnullptra null reference (Nothing in Visual Basic).

-or-

contentStream is nullNothingnullptra null reference (Nothing in Visual Basic).

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.

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 e-mail attachment.
Public Shared Sub SendErrorLog(ByVal server As String, ByVal recipientList As String)
    ' Create a message from logMailer@contoso.com to recipientList.
    Dim message As New MailMessage("logMailer@contoso.com", recipientList)

    message.Subject = "Error Log report"
    Dim fileName As String = "log.txt"
    ' Get the file stream for the error log.
    ' Requires the System.IO namespace.
    Dim fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)
    Dim s As New StreamReader(fs)
    Dim errors As Integer = 0
    Do While s.ReadLine() IsNot Nothing
        ' Process each line from the log file here.
        errors += 1
    Loop
    ' The e-mail message summarizes the data found in the log.
    message.Body = String.Format("{0} errors in log as of {1}", errors, Date.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.
    Dim ct As New ContentType(MediaTypeNames.Text.Plain)
    ' Attach the log file stream to the e-mail message.
    Dim data As New Attachment(fs, ct)
    Dim disposition As ContentDisposition = data.ContentDisposition
    ' Suggest a file name for the attachment.
    disposition.FileName = "log" & Date.Now.ToString() & ".txt"
    ' Add the attachment to the message.
    message.Attachments.Add(data)
    ' Send the message.
    ' Include credentials if the server requires them.
    Dim client As New SmtpClient(server)
    client.Credentials = CredentialCache.DefaultNetworkCredentials

    Try
        client.Send(message)
    Catch ex As Exception
        Console.WriteLine("Exception caught in SendErrorLog: {0}", ex.ToString())
    End Try
    data.Dispose()
    ' Close the log file.
    fs.Close()
End Sub


		// The following example sends a summary of a log file as the message
		// and the log as an e-mail 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 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.
			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 e-mail 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();
		}


   // The following example sends a summary of a log file as the message
   // and the log as an e-mail attachment.
   static void SendErrorLog( String^ server, String^ recipientList )
   {

      // Create a message from logMailer@contoso.com to recipientList.
      MailMessage^ message = gcnew MailMessage( L"logMailer@contoso.com",recipientList );
      message->Subject = L"Error Log report";
      String^ fileName = L"log.txt";

      // Get the file stream for the error log.
      // Requires the System.IO namespace.
      FileStream^ fs = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
      StreamReader^ s = gcnew StreamReader( fs );
      int errors = 0;
      while ( s->ReadLine() != nullptr )
      {

         // Process each line from the log file here.
         errors++;
      }

      message->Body = String::Format( L"{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 = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );

      // Make a contentType indicating that the log data
      // that is attached is plain text.
      ContentType^ ct = gcnew ContentType( MediaTypeNames::Text::Plain );

      // Attach the log file stream to the e-mail message.
      Attachment^ data = gcnew Attachment( fs,ct );
      ContentDisposition^ disposition = data->ContentDisposition;

      // Suggest a file name for the attachment.
      disposition->FileName = String::Format( L"log{0}.txt", DateTime::Now );

      // Add the attachment to the message.
      message->Attachments->Add( data );

      // Send the message.
      // Include credentials if the server requires them.
      SmtpClient^ client = gcnew SmtpClient( server );
      client->Credentials = CredentialCache::DefaultNetworkCredentials;
      client->Send( message );
      data->~Attachment();
      client->~SmtpClient();

      // Close the log file.
      fs->Close();
   }



Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.