Attachment 類別

定義

表示電子郵件的附件。

public ref class Attachment : System::Net::Mail::AttachmentBase
public class Attachment : System.Net.Mail.AttachmentBase
type Attachment = class
    inherit AttachmentBase
Public Class Attachment
Inherits AttachmentBase
繼承
Attachment

範例

下列程式代碼範例示範如何將檔案附加至電子郵件訊息。

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 集合。

附件內容可以是 StringStream或檔案名。 您可以使用任何 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 內容識別碼。

(繼承來源 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 AttachmentBase)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於