APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported. To determine whether an API is available in v1.0, use the Version selector.
Create a messageRule object by specifying a set of conditions and actions.
Outlook carries out those actions if an incoming message in the user's Inbox meets the specified conditions.
Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.
Permission type
Least privileged permissions
Higher privileged permissions
Delegated (work or school account)
MailboxSettings.ReadWrite
Not available.
Delegated (personal Microsoft account)
MailboxSettings.ReadWrite
Not available.
Application
MailboxSettings.ReadWrite
Not available.
HTTP request
POST /me/mailFolders/inbox/messageRules
POST /users/{id | userPrincipalName}/mailFolders/inbox/messageRules
In the request body, supply the parameters that are applicable to your rule. The following are body parameters that are typically used
when creating rules. You can specify any other writable messageRule properties as appropriate in the request body.
// 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);
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
// 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.NewMessageRule()
displayName := "From partner"
requestBody.SetDisplayName(&displayName)
sequence := int32(2)
requestBody.SetSequence(&sequence)
isEnabled := true
requestBody.SetIsEnabled(&isEnabled)
conditions := graphmodels.NewMessageRulePredicates()
senderContains := []string {
"adele",
}
conditions.SetSenderContains(senderContains)
requestBody.SetConditions(conditions)
actions := graphmodels.NewMessageRuleActions()
recipient := graphmodels.NewRecipient()
emailAddress := graphmodels.NewEmailAddress()
name := "Alex Wilbur"
emailAddress.SetName(&name)
address := "AlexW@contoso.com"
emailAddress.SetAddress(&address)
recipient.SetEmailAddress(emailAddress)
forwardTo := []graphmodels.Recipientable {
recipient,
}
actions.SetForwardTo(forwardTo)
stopProcessingRules := true
actions.SetStopProcessingRules(&stopProcessingRules)
requestBody.SetActions(actions)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
messageRules, err := graphClient.Me().MailFolders().ByMailFolderId("mailFolder-id").MessageRules().Post(context.Background(), requestBody, nil)
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
// 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);
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
<?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();
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
Import-Module Microsoft.Graph.Beta.Mail
$params = @{
displayName = "From partner"
sequence = 2
isEnabled = $true
conditions = @{
senderContains = @(
"adele"
)
}
actions = @{
forwardTo = @(
@{
emailAddress = @{
name = "Alex Wilbur"
address = "AlexW@contoso.com"
}
}
)
stopProcessingRules = $true
}
}
# A UPN can also be used as -UserId.
New-MgBetaUserMailFolderMessageRule -UserId $userId -MailFolderId $mailFolderId -BodyParameter $params
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.
# 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_rule import MessageRule
from msgraph_beta.generated.models.message_rule_predicates import MessageRulePredicates
from msgraph_beta.generated.models.message_rule_actions import MessageRuleActions
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 = MessageRule(
display_name = "From partner",
sequence = 2,
is_enabled = True,
conditions = MessageRulePredicates(
sender_contains = [
"adele",
],
),
actions = MessageRuleActions(
forward_to = [
Recipient(
email_address = EmailAddress(
name = "Alex Wilbur",
address = "AlexW@contoso.com",
),
),
],
stop_processing_rules = True,
),
)
result = await graph_client.me.mail_folders.by_mail_folder_id('mailFolder-id').message_rules.post(request_body)
Important
Microsoft Graph SDKs use the v1.0 version of the API by default, and do not support all the types, properties, and APIs available in the beta version. For details about accessing the beta API with the SDK, see Use the Microsoft Graph SDKs with the beta API.