共用方式為


ContentType.MediaType 屬性

定義

取得或設定媒體類型值,其包含在這個執行個體所表示的內容類型標頭中。

public:
 property System::String ^ MediaType { System::String ^ get(); void set(System::String ^ value); };
public string MediaType { get; set; }
member this.MediaType : string with get, set
Public Property MediaType As String

屬性值

String,包含媒體類型與子類型值。 這個值未包含接在子類型後面的分號 (;) 分隔符號。

例外狀況

為設定作業指定的值為 null

為設定作業指定的值是 Empty ("")。

為設定作業指定的值採用無法剖析的格式。

範例

下列程式代碼範例會設定這個屬性的值。

static void CreateMessageInlineAttachment2( String^ server, String^ textMessage )
{
   
   // Create a message and set up the recipients.
   MailMessage^ message = gcnew MailMessage( L"jane@contoso.com",L"ben@contoso.com",L"A text message for you.",L"Message: " );
   
   // Attach the message string to this email message.
   Attachment^ data = gcnew Attachment( textMessage );
   
   // Send textMessage as part of the email body.
   message->Attachments->Add( data );
   ContentType^ content = data->ContentType;
   content->MediaType = MediaTypeNames::Text::Plain;
   
   //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();
}
public static void CreateMessageInlineAttachment2(string server, string
textMessage)
{
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "jane@contoso.com",
       "ben@contoso.com",
       "A text message for you.",
       "Message: ");

    // Attach the message string to this email message.
    Attachment data = new Attachment(textMessage);
    // Send textMessage as part of the email body.
    message.Attachments.Add(data);
    ContentType content = data.ContentType;
    content.MediaType = MediaTypeNames.Text.Plain;
    //Send the message.
    // Include credentials if the server requires them.
    SmtpClient client = new SmtpClient(server);
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateMessageInlineAttachment2: {0}",
            ex.ToString());
    }
    data.Dispose();
}

備註

在下列 Content-Type 標頭範例中, MediaType 屬性值為 "application/x-myType"

content-type: application/x-myType; name=data.xyz

將此屬性設定為 nullString.Empty ,以從標頭中移除名稱資訊。

Content-Type 標頭的語法描述於 RFC 2045 第 5.1 節。 RFC 2046 提供有關MIME媒體類型的詳細資訊。 這些 RFC 可在取得 https://www.ietf.org

適用於