Les API sous la version /beta dans Microsoft Graph sont susceptibles d’être modifiées. L’utilisation de ces API dans des applications de production n’est pas prise en charge. Pour déterminer si une API est disponible dans v1.0, utilisez le sélecteur Version .
Créer un brouillon d’un nouveau message au format JSON ou MIME.
Lorsque vous utilisez le format JSON, vous pouvez :
*
Note: Les charges utiles des messages S/MIME sont actuellement limitées à 4 Mo. Les tentatives de soumission qui dépassent cette limite entraîneront uneHTTP 413 Request Entity Too Large réponse d’erreur.
L’appel de cette API nécessite une des autorisations suivantes. Pour plus d’informations, notamment sur la façon de choisir les autorisations, voir Autorisations.
Type d’autorisation
Autorisations (de celle qui offre le plus de privilèges à celle qui en offre le moins)
Déléguée (compte professionnel ou scolaire)
Mail.ReadWrite
Déléguée (compte Microsoft personnel)
Mail.ReadWrite
Application
Mail.ReadWrite
Requête HTTP
POST /me/messages
POST /users/{id|userPrincipalName}/messages
POST /me/mailFolders/{id}/messages
POST /users/{id | userPrincipalName}/mailFolders/{id}/messages
Nature des données dans le corps d’une entité. Obligatoire. Utiliser application/json pour un objet JSON et text/plain pour du contenu MIME
Corps de la demande
Lorsque vous utilisez le format JSON, fournissez une représentation JSON de l’objet message .
Lorsque vous spécifiez le corps au format MIME, fournissez au contenu MIME les en-têtes de messages Internet applicables (« À », « CC », « Cci », « Objet »), tous codés au format base64 dans le corps de la demande.
Pour utiliser la mention pour appeler un autre utilisateur dans le nouveau message :
Incluez la propriété toRecipients requise, la propriété mentions et toutes les propriétés de message accessibles en écriture dans le corps de la demande.
Pour chaque mention dans la propriété mentions , vous devez spécifier la propriété mentionnée .
Dans la mesure où la ressource message prend en charge des extensions, vous pouvez utiliser l’opération POST pour ajouter des propriétés personnalisées avec vos propres données au message lors de sa création.
Réponse
Si elle réussit, cette méthode renvoie un 201 Created code de réponse et un objet message dans le corps de la réponse.
Si le corps de la demande inclut du contenu MIME malformé, cette méthode renvoie 400 Bad request et le message d’erreur suivant : « Chaîne base64 non valide pour le contenu MIME ».
Exemples
Exemple 1 : Créer un brouillon de message au format JSON
Demande
L’exemple suivant montre une demande de création d’un brouillon d’un nouveau message.
POST https://graph.microsoft.com/beta/me/messages
Content-type: application/json
{
"subject":"Did you see last night's game?",
"importance":"Low",
"body":{
"contentType":"HTML",
"content":"They were <b>awesome</b>!"
},
"toRecipients":[
{
"emailAddress":{
"address":"AdeleV@contoso.com"
}
}
]
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new Message
{
Subject = "Did you see last night's game?",
Importance = Importance.Low,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "They were <b>awesome</b>!",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AdeleV@contoso.com",
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Messages.PostAsync(requestBody);
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewMessage()
subject := "Did you see last night's game?"
requestBody.SetSubject(&subject)
importance := graphmodels.LOW_IMPORTANCE
requestBody.SetImportance(&importance)
body := graphmodels.NewItemBody()
contentType := graphmodels.HTML_BODYTYPE
body.SetContentType(&contentType)
content := "They were <b>awesome</b>!"
body.SetContent(&content)
requestBody.SetBody(body)
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
address := "AdeleV@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
toRecipients := []graphmodels.Recipientable {
recipient,
}
requestBody.SetToRecipients(toRecipients)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
messages, err := graphClient.Me().Messages().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Message message = new Message();
message.setSubject("Did you see last night's game?");
message.setImportance(Importance.Low);
ItemBody body = new ItemBody();
body.setContentType(BodyType.Html);
body.setContent("They were <b>awesome</b>!");
message.setBody(body);
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress("AdeleV@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
Message result = graphClient.me().messages().post(message);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\Importance;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
use Microsoft\Graph\Beta\Generated\Models\BodyType;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Message();
$requestBody->setSubject('Did you see last night\'s game?');
$requestBody->setImportance(new Importance('low'));
$body = new ItemBody();
$body->setContentType(new BodyType('hTML'));
$body->setContent('They were <b>awesome</b>!');
$requestBody->setBody($body);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('AdeleV@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$requestBody->setToRecipients($toRecipientsArray);
$result = $graphServiceClient->me()->messages()->post($requestBody)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.importance import Importance
from msgraph_beta.generated.models.item_body import ItemBody
from msgraph_beta.generated.models.body_type import BodyType
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Message(
subject = "Did you see last night's game?",
importance = Importance.Low,
body = ItemBody(
content_type = BodyType.Html,
content = "They were <b>awesome</b>!",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "AdeleV@contoso.com",
),
),
],
)
result = await graph_client.me.messages.post(request_body)
Exemple 2 : Créer un brouillon de message qui inclut une @-mention
Demande
L’exemple suivant montre un brouillon d’e-mail de Randi Welch à Samantha Booth. Le message inclut également une mention d’un autre utilisateur, Dana Swope.
Dans le corps de la demande, fournissez une représentation JSON de l’objet message.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new Message
{
Subject = "Party planning",
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Name = "Samantha Booth",
Address = "samanthab@contoso.com",
},
},
},
Mentions = new List<Mention>
{
new Mention
{
Mentioned = new EmailAddress
{
Name = "Dana Swope",
Address = "danas@contoso.com",
},
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Messages.PostAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Message message = new Message();
message.setSubject("Party planning");
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setName("Samantha Booth");
emailAddress.setAddress("samanthab@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<Mention> mentions = new LinkedList<Mention>();
Mention mention = new Mention();
EmailAddress mentioned = new EmailAddress();
mentioned.setName("Dana Swope");
mentioned.setAddress("danas@contoso.com");
mention.setMentioned(mentioned);
mentions.add(mention);
message.setMentions(mentions);
Message result = graphClient.me().messages().post(message);
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
from msgraph_beta.generated.models.mention import Mention
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Message(
subject = "Party planning",
to_recipients = [
Recipient(
email_address = EmailAddress(
name = "Samantha Booth",
address = "samanthab@contoso.com",
),
),
],
mentions = [
Mention(
mentioned = EmailAddress(
name = "Dana Swope",
address = "danas@contoso.com",
),
),
],
)
result = await graph_client.me.messages.post(request_body)
L’exemple suivant illustre la réponse. Remarque : L’objet de réponse présenté ici est tronqué par souci de concision. Toutes les propriétés sont renvoyées à partir d’un appel réel.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new Message
{
Subject = "9/8/2018: concert",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "The group represents Washington.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "AlexW@contoso.com",
},
},
},
InternetMessageHeaders = new List<InternetMessageHeader>
{
new InternetMessageHeader
{
Name = "x-custom-header-group-name",
Value = "Washington",
},
new InternetMessageHeader
{
Name = "x-custom-header-group-id",
Value = "WA001",
},
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.Messages.PostAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Message message = new Message();
message.setSubject("9/8/2018: concert");
ItemBody body = new ItemBody();
body.setContentType(BodyType.Html);
body.setContent("The group represents Washington.");
message.setBody(body);
LinkedList<Recipient> toRecipients = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress("AlexW@contoso.com");
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
LinkedList<InternetMessageHeader> internetMessageHeaders = new LinkedList<InternetMessageHeader>();
InternetMessageHeader internetMessageHeader = new InternetMessageHeader();
internetMessageHeader.setName("x-custom-header-group-name");
internetMessageHeader.setValue("Washington");
internetMessageHeaders.add(internetMessageHeader);
InternetMessageHeader internetMessageHeader1 = new InternetMessageHeader();
internetMessageHeader1.setName("x-custom-header-group-id");
internetMessageHeader1.setValue("WA001");
internetMessageHeaders.add(internetMessageHeader1);
message.setInternetMessageHeaders(internetMessageHeaders);
Message result = graphClient.me().messages().post(message);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\Message;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
use Microsoft\Graph\Beta\Generated\Models\BodyType;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
use Microsoft\Graph\Beta\Generated\Models\InternetMessageHeader;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new Message();
$requestBody->setSubject('9/8/2018: concert');
$body = new ItemBody();
$body->setContentType(new BodyType('hTML'));
$body->setContent('The group represents Washington.');
$requestBody->setBody($body);
$toRecipientsRecipient1 = new Recipient();
$toRecipientsRecipient1EmailAddress = new EmailAddress();
$toRecipientsRecipient1EmailAddress->setAddress('AlexW@contoso.com');
$toRecipientsRecipient1->setEmailAddress($toRecipientsRecipient1EmailAddress);
$toRecipientsArray []= $toRecipientsRecipient1;
$requestBody->setToRecipients($toRecipientsArray);
$internetMessageHeadersInternetMessageHeader1 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader1->setName('x-custom-header-group-name');
$internetMessageHeadersInternetMessageHeader1->setValue('Washington');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader1;
$internetMessageHeadersInternetMessageHeader2 = new InternetMessageHeader();
$internetMessageHeadersInternetMessageHeader2->setName('x-custom-header-group-id');
$internetMessageHeadersInternetMessageHeader2->setValue('WA001');
$internetMessageHeadersArray []= $internetMessageHeadersInternetMessageHeader2;
$requestBody->setInternetMessageHeaders($internetMessageHeadersArray);
$result = $graphServiceClient->me()->messages()->post($requestBody)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.message import Message
from msgraph_beta.generated.models.item_body import ItemBody
from msgraph_beta.generated.models.body_type import BodyType
from msgraph_beta.generated.models.recipient import Recipient
from msgraph_beta.generated.models.email_address import EmailAddress
from msgraph_beta.generated.models.internet_message_header import InternetMessageHeader
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Message(
subject = "9/8/2018: concert",
body = ItemBody(
content_type = BodyType.Html,
content = "The group represents Washington.",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "AlexW@contoso.com",
),
),
],
internet_message_headers = [
InternetMessageHeader(
name = "x-custom-header-group-name",
value = "Washington",
),
InternetMessageHeader(
name = "x-custom-header-group-id",
value = "WA001",
),
],
)
result = await graph_client.me.messages.post(request_body)
Dans le corps de la demande, fournissez une représentation JSON de l’objet message.
Réponse
L’exemple suivant illustre la réponse. Remarque : les en-têtes de message Internet ne sont pas renvoyés par défaut dans une réponse POST. L’objet réponse présenté ici peut également être tronqué par souci de concision. Toutes les propriétés sont renvoyées à partir d’un appel réel.