Share via


ContentType.ToString Méthode

Définition

Retourne une représentation sous forme de chaîne de cet objet ContentType.

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

Retours

String qui contient les paramètres actuels de ce ContentType.

Exemples

L’exemple de code suivant affiche la représentation sous forme de chaîne d’un 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();
}

Remarques

La valeur de la ToString méthode contient la chaîne utilisée pour créer ce ContentType, si une valeur a été spécifiée et qu’aucune valeur de propriété n’a été modifiée. Si aucune chaîne n’a été spécifiée ou si une ou plusieurs valeurs de propriété ont changé depuis ContentType sa création, la valeur de ToString est une chaîne au format d’un en-tête Content-Type composé à l’aide des valeurs de propriété actuelles. La chaîne n’inclut pas le "Content-type:" préfixe.

La syntaxe de l’en-tête Content-Type est décrite dans RFC 2045 Section 5.1, disponible à l’adresse https://www.ietf.org.

S’applique à