How to Approve and Reject Moderation Emails in Exchange Online with the Microsoft Graph API and Powershell
A while ago I published this blog post about doing this using EWS and a few people have recently asked if it is also possible to do this with the Graph API(which it is) so I've decided to include this one in my Graph Basics series.
Moderation
Moderation is an Exchange feature that was introduced in Exchange 2010 that allows the Human control of mail flow to a distribution group or mailbox see https://docs.microsoft.com/en-us/exchange/security-and-compliance/mail-flow-rules/manage-message-approval for more detail.Moderation Approve/Reject Messages
When a Message is requiring moderation an email is sent to one (or more) moderators requesting approval. In the Graph you can get these moderation messages by filtering on the MessageClass property. Because this isn't a first-class property like it was in EWS you need to use the singleValueExtendedProperties representation of the property. eg in the Graph a Request like this
To Approve or Reject a Message that requires moderation you need to do a few different things to create the approval/rejection Messagehttps://graph.microsoft.com/v1.0/me/mailFolders('Inbox')/messages? $filter=singleValueExtendedProperties/any(ep:ep/id eq 'String 0x001a' and ep/value eq 'IPM.Note.Microsoft.Approval.Request')
Approval -
{
id = "String 0x001A"
value = "IPM.Note.Microsoft.Approval.Reply.Aprove"
}
Rejection -{
id = "String 0x001A"
value = "IPM.Note.Microsoft.Approval.Reply.Reject"
}
2. Subject - Use the Normalized subject from the approval Request email and then prepend Accept or Reject 3. RecipientTo - Needs to be set to the Microsoft Exchange Approval Assistant Address (Sender of the Moderation Message)
4. VerbResponse - This tells Exchange if you want to Approve or Reject the Message
{
id = "String {00062008-0000-0000-C000-000000000046} Id 0x8524"
value = "Approve"
}
5. PidTagReportTag Property - This is a critical property you get from the Approval Request which is used to correlate the Approval Response.
{
id = "Binary 0x31"
value = $ApprovalMail.singleValueExtendedProperties[0].value
}
Once you have all that information you can put a message together in JSON and send that using the SendMail endpoint in the Microsoft GraphI've put a full sample script for this on GitHub https://github.com/gscales/Powershell-Scripts/blob/master/Graph101/Moderation.ps1
Invoke-ApproveModerationRequest will approve the last moderation request in a Mailbox
Invoke-RejectModerationRequest will reject the last moderation request in a Mailbox