Attachment Constructor (Stream, String)
Initializes a new instance of the Attachment class with the specified stream and name.
Assembly: System (in System.dll)
Parameters
- contentStream
- Type: System.IO::Stream
A readable Stream that contains the content for this attachment.
- name
- Type: System::String
A String that contains the value for the Name property of the ContentType associated with this attachment. This value can be nullptr.
| Exception | Condition |
|---|---|
| ArgumentNullException | contentStream is nullptr. |
If name is not nullptr 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.
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. static void SendNamedErrorLog( 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 and is named. ContentType^ ct = gcnew ContentType; ct->MediaType = MediaTypeNames::Text::Plain; ct->Name = String::Format( L"log{0}.txt", DateTime::Now ); // Create the attachment. Attachment^ data = gcnew 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 = gcnew SmtpClient( server ); client->Credentials = CredentialCache::DefaultNetworkCredentials; client->Send( message ); data->~Attachment(); client->~SmtpClient(); // Close the log file. fs->Close(); return; }
// The following example sends a summary of a log file as the message
// and the log as an e-mail attachment.
public:
static void SendNamedErrorLog(String* server, String* recipientList)
{
// Create a message from logMailer@contoso.com to recipientList.
MailMessage* message = new MailMessage(
S"logMailer@contoso.com", recipientList);
message->Subject = S"Error Log report";
String* fileName = S"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() != 0)
{
// Process each line from the log file here.
errors ++;
}
// The e-mail message summarizes the data found in the log.
message->Body = String::Format(S"{0} errors in log as of {1}", __box(errors), __box(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 = String::Format( S"log{0}.txt", __box(DateTime::Now));
// 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;
client->Send(message);
data->Dispose();
client->Dispose();
// Close the log file.
fs->Close();
return;
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.