ContentType.ToString 方法

定義

傳回這個 ContentType 物件的字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

String,包含這個 ContentType 的目前設定。

範例

下列程式代碼範例會顯示的 ContentType字串表示。

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

備註

方法的值 ToString 包含用來建立這個 ContentType的字串,如果指定了 ,而且沒有變更屬性值。 如果未指定任何字串,或自建立之後 ContentType 已變更一或多個屬性值,則的值 ToString 會是使用目前屬性值所組成 Content-Type 標頭格式的字串。 字串不包含前置 "Content-type:" 詞。

Content-Type 標頭的語法說明於 的 RFC 2045 第 5.1 節。https://www.ietf.org

適用於