Share via


ContentType.MediaType 属性

定义

获取或设置由此实例表示的 Content-Type 标头中包含的媒体类型值。

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 标头的以下示例中,属性"application/x-myType"MediaType值为 。

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

将此属性设置为 nullString.Empty 以从标头中删除名称信息。

RFC 2045 第 5.1 节介绍了 Content-Type 标头的语法。 RFC 2046 提供有关 MIME 媒体类型的详细信息。 这些 RFC 在 中可用 https://www.ietf.org

适用于