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で利用できます。

コンストラクター

名前 説明
ContentType()

ContentType クラスの新しい既定のインスタンスを初期化します。

ContentType(String)

指定した文字列を使用して、 ContentType クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Boundary

このインスタンスによって表される Content-Type ヘッダーに含まれる境界パラメーターの値を取得または設定します。

CharSet

このインスタンスによって表される Content-Type ヘッダーに含まれる charset パラメーターの値を取得または設定します。

MediaType

このインスタンスによって表される Content-Type ヘッダーに含まれるメディアの種類の値を取得または設定します。

Name

このインスタンスによって表される Content-Type ヘッダーに含まれる name パラメーターの値を取得または設定します。

Parameters

このインスタンスによって表される Content-Type ヘッダーに含まれるパラメーターを含むディクショナリを取得します。

メソッド

名前 説明
Equals(Object)

指定した ContentType オブジェクトのコンテンツ タイプ ヘッダーがこのオブジェクトのコンテンツ タイプ ヘッダーと等しいかどうかを判断します。

GetHashCode()

指定した ContentType オブジェクトのハッシュ コードを決定します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

この ContentType オブジェクトの文字列形式を返します。

適用対象

こちらもご覧ください