Attachment.Attachment(Stream, ContentType) Constructor
.NET Framework 3.0
Initializes a new instance of the Attachment class with the specified stream and content type.
Namespace: System.Net.Mail
Assembly: System (in system.dll)
Assembly: System (in system.dll)
public Attachment ( Stream contentStream, ContentType contentType )
public function Attachment ( contentStream : Stream, contentType : ContentType )
Not applicable.
Parameters
- contentStream
A readable Stream that contains the content for this attachment.
- contentType
A ContentType that describes the data in stream.
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.
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 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; client.Send(message); data.Dispose(); // Close the log file. fs.Close(); }
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: