One of my clients wanted to process mail in shared mail folders.
The process
Table of Contents
In short, the process that I want to implement is as follows.
An email arrives in a Shared Mailbox, and on arrival flow uses the subject of the email to create a folder in Shared Mailbox.
Triggering the flow on incoming emails
Every flow has to start with a trigger. For this flow I will use the When a new email arrives in a shared mailbox.
All I need to do is specify the mailbox address of my shared mailbox and then I could configure a filter. It might be wise to configure a filter here if you want to avoid any spam emails spoiling your flow runs.
Introduction to MS Graph mail Folders
In this post I will use a few Microsoft Graph calls.
The first one is:
https://graph.microsoft.com/v1.0/me/MailFolders
A GET to the above will result in a list of folders in the mailbox of the currently logged in user.
A Post to the above will result in a new folder being created in a mailbox or folder.
Graph Explorer
I useful tool to try these endpoint out is MS Graph Explorer.
All my folders are listed and I can now use this data to do things. time to have a look at creating a flow.
Setting up the permissions
The flow that I’m going to create will use an app registration in Azure. I will refer to Serge Luca’s post about app registrations.
In our case however we will need slightly different permissions assigned.
The permissions that we will need today are Mail.ReadWrite
Make sure that you use application permissions and not the delegate permissions.
Creating a flow with mail folders
Using the above mentioned post, we now have a flow that looks like this. I secured the input of the Client, Tenant and Secret so that they don’t appear in my flow runs.
Then the Get Access token action will give us an access token for the stuff that we want to do with mailfolders.
Getting mail folder details
I’ve now created a compose action that collects the access token from the previously mentioned HTTP request action. the compose needs to be set to the following expression.
body('Get_Access_token')?['access_token']
Now I’m going to create two parallel actions one using
https://graph.microsoft.com/v1.0/me/MailFolders/
and the otherone will be using
https://graph.microsoft.com/v1.0/users/mysharedmailbox@mytenant.onmicrosoft.com/MailFolders
The first option gave me a good result in MS Graph explorer, however within Flow i’m having to use /user/userid instead.
The /Me problem
When i try to access the /me en endpoints I get the following error message returned.
Current authenticated context is not valid for this request. This occurs when a request is made to an endpoint that requires user sign-in. For example, /me requires a signed-in user. Acquire a token on behalf of a user to make requests to these endpoints. Use the OAuth 2.0 authorization code flow for mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps.
Calling graph APIs in/me from a flow simply seems to cause problems. But, in this case i don’t need to worry too much. I’m happy to use /user/userid/mailFodlers instead. Even if I try to access /me. Simply use my own user id and it will work.
Shared Mailboxes
When you access shared mailboxes there will be a user id for the shared mailbox. This user id will match the email address used hence the following url will help us to get to the folders.
https://graph.microsoft.com/v1.0/users/mysharedmailbox@mytenant.onmicrosoft.com/MailFolders
Creating a flow to create a Mail Folder
To create a new subfolder we can use the following end point
Now all we have to do is collect the subject of the email and use it in the displayName of the above HTTP request. But, that is an easy step.
Other issues found on the way
At some point I got the following error:
CompactToken parsing failed with error code: 80049217
This means that you have messed up something with the bearer token. In my case I added an extra character to it, making the token invalid.