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éez un objet messageRule en spécifiant un ensemble de conditions et d’actions.
Outlook effectue ces actions si un message entrant dans la boîte de réception de l’utilisateur remplit les conditions spécifiées.
Dans le corps de la demande, indiquez les paramètres qui s’appliquent à votre règle. Vous trouverez ci-dessous les paramètres de corps qui sont généralement utilisés lors de la création de règles. Vous pouvez spécifier d’autres propriétés messageRule accessibles en écriture selon vos besoins dans le corps de la demande.
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new MessageRule
{
DisplayName = "From partner",
Sequence = 2,
IsEnabled = true,
Conditions = new MessageRulePredicates
{
SenderContains = new List<string>
{
"adele",
},
},
Actions = new MessageRuleActions
{
ForwardTo = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Name = "Alex Wilbur",
Address = "AlexW@contoso.com",
},
},
},
StopProcessingRules = true,
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.MailFolders["{mailFolder-id}"].MessageRules.PostAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
MessageRule messageRule = new MessageRule();
messageRule.setDisplayName("From partner");
messageRule.setSequence(2);
messageRule.setIsEnabled(true);
MessageRulePredicates conditions = new MessageRulePredicates();
LinkedList<String> senderContains = new LinkedList<String>();
senderContains.add("adele");
conditions.setSenderContains(senderContains);
messageRule.setConditions(conditions);
MessageRuleActions actions = new MessageRuleActions();
LinkedList<Recipient> forwardTo = new LinkedList<Recipient>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setName("Alex Wilbur");
emailAddress.setAddress("AlexW@contoso.com");
recipient.setEmailAddress(emailAddress);
forwardTo.add(recipient);
actions.setForwardTo(forwardTo);
actions.setStopProcessingRules(true);
messageRule.setActions(actions);
MessageRule result = graphClient.me().mailFolders().byMailFolderId("{mailFolder-id}").messageRules().post(messageRule);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\MessageRule;
use Microsoft\Graph\Beta\Generated\Models\MessageRulePredicates;
use Microsoft\Graph\Beta\Generated\Models\MessageRuleActions;
use Microsoft\Graph\Beta\Generated\Models\Recipient;
use Microsoft\Graph\Beta\Generated\Models\EmailAddress;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new MessageRule();
$requestBody->setDisplayName('From partner');
$requestBody->setSequence(2);
$requestBody->setIsEnabled(true);
$conditions = new MessageRulePredicates();
$conditions->setSenderContains(['adele', ]);
$requestBody->setConditions($conditions);
$actions = new MessageRuleActions();
$forwardToRecipient1 = new Recipient();
$forwardToRecipient1EmailAddress = new EmailAddress();
$forwardToRecipient1EmailAddress->setName('Alex Wilbur');
$forwardToRecipient1EmailAddress->setAddress('AlexW@contoso.com');
$forwardToRecipient1->setEmailAddress($forwardToRecipient1EmailAddress);
$forwardToArray []= $forwardToRecipient1;
$actions->setForwardTo($forwardToArray);
$actions->setStopProcessingRules(true);
$requestBody->setActions($actions);
$result = $graphServiceClient->me()->mailFolders()->byMailFolderId('mailFolder-id')->messageRules()->post($requestBody)->wait();