Create rule

Important

APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.

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.

Permissions

One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions.

Permission type Permissions (from least to most privileged)
Delegated (work or school account) MailboxSettings.ReadWrite
Delegated (personal Microsoft account) MailboxSettings.ReadWrite
Application MailboxSettings.ReadWrite

HTTP request

POST /me/mailFolders/inbox/messagerules
POST /users/{id | userPrincipalName}/mailFolders/inbox/messagerules

Request headers

Name Description
Authorization Bearer {token}. Required.

Request body

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.

Parameter Type Description
actions messageRuleActions Actions to be taken on a message when the corresponding conditions, if any, are fulfilled. Required.
conditions messageRulePredicates Conditions that when fulfilled, will trigger the corresponding actions for that rule. Optional.
displayName String The display name of the rule. Required.
exceptions messageRulePredicates Represents exception conditions for the rule. Optional.
isEnabled Boolean Indicates whether the rule is enabled to be applied to messages. Optional.
sequence Int32 Indicates the order in which the rule is executed, among other rules. Required.

Response

If successful, this method returns 201 Created response code and a messageRule object in the response body.

Example

Request

Here is an example of the request.

POST https://graph.microsoft.com/beta/me/mailFolders/inbox/messagerules
Content-type: application/json

{      
    "displayName": "From partner",      
    "sequence": 2,      
    "isEnabled": true,          
    "conditions": {
        "senderContains": [
          "adele"       
        ]
     },
     "actions": {
        "forwardTo": [
          {
             "emailAddress": {
                "name": "Alex Wilbur",
                "address": "AlexW@contoso.onmicrosoft.com"
              }
           }
        ],
        "stopProcessingRules": true
     }    
}

Response

Here is an example of the response. Note: The response object shown here may be truncated for brevity. All of the properties will be returned from an actual call.

HTTP/1.1 201 Created
Content-type: application/json

{
  "id":"AQAAAJ5dZqA=",
  "displayName":"From partner",
  "sequence":2,
  "isEnabled":true,
  "hasError":false,
  "isReadOnly":false,
  "conditions":{
    "senderContains":[
      "ADELE"
    ]
  },
  "actions":{
      "stopProcessingRules":true,
      "forwardTo":[
        {
          "emailAddress":{
            "name":"Alex Wilbur",
            "address":"AlexW@contoso.onmicrosoft.com"
          }
        }
      ]
  }
}

SDK sample code


GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var messageRule = 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.onmicrosoft.com"
				}
			}
		},
		StopProcessingRules = true
	}
};

await graphClient.Me.MailFolders["inbox"].MessageRules
	.Request()
	.AddAsync(messageRule);

Read the SDK documentation for details on how to add the SDK to your project and create an authProvider instance.