ContentType 類別

定義

代表 MIME 協定的 Content-Type 標頭。

public ref class ContentType
public class ContentType
type ContentType = class
Public Class ContentType
繼承
ContentType

範例

以下程式碼範例是發送帶有附件的電子郵件,並顯示 ContentDisposition 附件的屬性。

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();
}

備註

課程中的 ContentType 資訊用來描述電子郵件中包含的資料,使顯示電子郵件的軟體能以適當方式呈現內容。 ContentType 與類別一起使用 Attachment ,用以指定附件中內容的類型。

Content-Type 標頭的語法詳見 RFC 2045 第 5.1 節。 RFC 2046 提供了關於 MIME 媒體類型及其參數的詳細資訊。 這些 RFC 可於 https://www.ietf.org

建構函式

名稱 Description
ContentType()

初始化該類別的新預設實例 ContentType

ContentType(String)

使用指定的字串初始化該類別的新實例 ContentType

屬性

名稱 Description
Boundary

取得或設定此實例所代表的 Content-Type 標頭中包含的邊界參數值。

CharSet

取得或設定此實例所代表的 Content-Type 標頭中包含的字元集參數值。

MediaType

取得或設定本實例所代表的內容型標頭中包含的媒體類型值。

Name

取得或設定此實例所代表的 Content-Type 標頭中包含的名稱參數值。

Parameters

取得包含此實例 Content-Type 標頭中參數的字典。

方法

名稱 Description
Equals(Object)

判斷指定 ContentType 物件的內容型態標頭是否等於該物件的內容型別標頭。

GetHashCode()

決定指定 ContentType 物件的雜湊碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

回傳此 ContentType 物件的字串表示。

適用於

另請參閱