Attachment 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示电子邮件的附件。
public ref class Attachment : System::Net::Mail::AttachmentBasepublic class Attachment : System.Net.Mail.AttachmentBasetype Attachment = class
    inherit AttachmentBasePublic Class Attachment
Inherits AttachmentBase- 继承
示例
下面的代码示例演示如何将文件附加到电子邮件。
static void CreateMessageWithAttachment( String^ server )
{
   
   // Specify the file to be attached and sent.
   // This example assumes that a file named Data.xls exists in the
   // current working directory.
   String^ file = L"data.xls";
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"Quarterly data report.",L"See the attached spreadsheet." );
   
   // Create  the file attachment for this email message.
   Attachment^ data = gcnew Attachment(file, MediaTypeNames::Application::Octet);
   
   // Add time stamp information for the file.
   ContentDisposition^ disposition = data->ContentDisposition;
   disposition->CreationDate = System::IO::File::GetCreationTime( file );
   disposition->ModificationDate = System::IO::File::GetLastWriteTime( file );
   disposition->ReadDate = System::IO::File::GetLastAccessTime( file );
   
   // Add the file attachment to this email message.
   message->Attachments->Add( data );
   
   //Send the message.
   SmtpClient^ client = gcnew SmtpClient( server );
   
   // Add credentials if the SMTP server requires them.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   client->Send( message );
   
   // Display the values in the ContentDisposition for the attachment.
   ContentDisposition^ cd = data->ContentDisposition;
   Console::WriteLine( L"Content disposition" );
   Console::WriteLine( cd );
   Console::WriteLine( L"File {0}", cd->FileName );
   Console::WriteLine( L"Size {0}", cd->Size );
   Console::WriteLine( L"Creation {0}", cd->CreationDate );
   Console::WriteLine( L"Modification {0}", cd->ModificationDate );
   Console::WriteLine( L"Read {0}", cd->ReadDate );
   Console::WriteLine( L"Inline {0}", cd->Inline );
   Console::WriteLine( L"Parameters: {0}", cd->Parameters->Count );
   IEnumerator^ myEnum1 = cd->Parameters->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      DictionaryEntry^ d = safe_cast<DictionaryEntry^>(myEnum1->Current);
      Console::WriteLine( L"{0} = {1}", d->Key, d->Value );
   }
   data->~Attachment();
   client->~SmtpClient();
}
public static void CreateMessageWithAttachment(string server)
{
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = "data.xls";
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
        "jane@contoso.com",
        "ben@contoso.com",
        "Quarterly data report.",
        "See the attached spreadsheet.");
    // Create  the file attachment for this email message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
    // Add the file attachment to this email message.
    message.Attachments.Add(data);
    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
            ex.ToString());
    }
    // Display the values in the ContentDisposition for the attachment.
    ContentDisposition cd = data.ContentDisposition;
    Console.WriteLine("Content disposition");
    Console.WriteLine(cd.ToString());
    Console.WriteLine("File {0}", cd.FileName);
    Console.WriteLine("Size {0}", cd.Size);
    Console.WriteLine("Creation {0}", cd.CreationDate);
    Console.WriteLine("Modification {0}", cd.ModificationDate);
    Console.WriteLine("Read {0}", cd.ReadDate);
    Console.WriteLine("Inline {0}", cd.Inline);
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
    foreach (DictionaryEntry d in cd.Parameters)
    {
        Console.WriteLine("{0} = {1}", d.Key, d.Value);
    }
    data.Dispose();
}
注解
类 Attachment 与 类一 MailMessage 起使用。 所有消息都包含 一个 Body,其中包含消息的内容。 除了正文,你可能还想要发送其他文件。 它们作为附件发送,并表示为 Attachment 实例。 若要向邮件添加附件,请将其添加到集合。MailMessage.Attachments
附件内容可以是 String、 Stream或文件名。 可以使用任何 Attachment 构造函数指定附件中的内容。
附件的 MIME Content-Type 标头信息由 ContentType 属性表示。 Content-Type 标头指定媒体类型和子类型以及任何关联的参数。 使用 ContentType 获取与附件关联的实例。
MIME Content-Disposition 标头由 ContentDisposition 属性表示。 Content-Disposition 标头指定附件的演示文稿和文件时间戳。 仅当附件是文件时,才会发送 Content-Disposition 标头。 ContentDisposition使用 属性获取与附件关联的实例。
MIME Content-Transfer-Encoding 标头由 TransferEncoding 属性表示。
构造函数
| Attachment(Stream, ContentType) | 使用指定的流和内容类型初始化 Attachment 类的新实例。 | 
| Attachment(Stream, String) | 使用指定的流和名称初始化 Attachment 类的新实例。 | 
| Attachment(Stream, String, String) | 使用指定的流、名称和 MIME 类型信息初始化 Attachment 类的新实例。 | 
| Attachment(String) | 使用指定的内容字符串初始化 Attachment 类的新实例。 | 
| Attachment(String, ContentType) | 使用指定的内容字符串和 Attachment 初始化 ContentType 类的新实例。 | 
| Attachment(String, String) | 使用指定的内容字符串和 MIME 类型信息初始化 Attachment 类的新实例。 | 
属性
| ContentDisposition | 获取此附件的 MIME 内容处置。 | 
| ContentId | 获取或设置此附件的 MIME 内容 ID。(继承自 AttachmentBase) | 
| ContentStream | 获取此附件的内容流。(继承自 AttachmentBase) | 
| ContentType | 获取此附件的内容类型。(继承自 AttachmentBase) | 
| Name | 获取或设置与此附件关联的内容类型中的 MIME 内容类型名称值。 | 
| NameEncoding | 指定用于 AttachmentName 的编码。 | 
| TransferEncoding | 获取或设置此附件的编码。(继承自 AttachmentBase) | 
方法
| CreateAttachmentFromString(String, ContentType) | 使用指定字符串中的内容和指定的 ContentType 创建邮件附件。 | 
| CreateAttachmentFromString(String, String) | 使用指定字符串中的内容和指定的 MIME 内容类型名创建邮件附件。 | 
| CreateAttachmentFromString(String, String, Encoding, String) | 使用指定字符串中的内容、指定的 MIME 内容类型名、字符编码和附件的 MIME 标头信息创建邮件附件。 | 
| Dispose() | 释放由 AttachmentBase 占用的资源。(继承自 AttachmentBase) | 
| Dispose(Boolean) | 释放由 AttachmentBase 占用的非托管资源,还可以另外再释放托管资源。(继承自 AttachmentBase) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |