// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.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\GraphServiceClient;
use Microsoft\Graph\Generated\Models\MessageRule;
use Microsoft\Graph\Generated\Models\MessageRulePredicates;
use Microsoft\Graph\Generated\Models\MessageRuleActions;
use Microsoft\Graph\Generated\Models\Recipient;
use Microsoft\Graph\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();