Share via


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 作成されてから 1 つ以上のプロパティ値が変更された場合、 の ToString 値は、現在のプロパティ値を使用して構成される Content-Type ヘッダーの形式の文字列です。 文字列にはプレフィックスは "Content-type:" 含まれません。

Content-Type ヘッダーの構文については、RFC 2045 セクション 5.1 () で https://www.ietf.org説明されています。

適用対象