Home > Mobile >  Microsoft graph API how to query mails sent to specific address
Microsoft graph API how to query mails sent to specific address

Time:09-06

I want to query my outlook messages for specific adresses that I have sent my mails using the documentation here I was able to construct the query below. However, it returns a 400 error code and I do not know how to proceed.

https://graph.microsoft.com/v1.0/Users/{user}/messages/?$filter=toRecipients/any(t:t/emailAddress/address eq '{target_email}')

for reference, this is how messages are structured:

url = https://graph.microsoft.com/v1.0/Users/{user}/messages/?$filter=from/emailAddress/address eq '[email protected]'

messages = requests.get(url, headers=headers)
message = r['value'][0]

print(message['from'])
print(message['toRecipients'])

{'emailAddress': {'name': 'john', 'address': '[email protected]'}}
[{'emailAddress': {'name': 'sara', 'address': '[email protected]'}}]

The idea being that I filter on toRecipients, iterate over all recipients and look for a match in the given emailAdress. How should I do this the correct way?

CodePudding user response:

The toRecipients doesn't work the same as the from address because one is just a property of type microsoft.graph.emailaddress and the other is a collection of those properties (technically this is part of the recipients collection in exchange which also includes CC objects so it's a nested collection).

Those properties are indexed so you can use Search to find messages eg

/me/messages?$search="to:[email protected]"&$select=subject,toRecipients

or you could use participants which include from,to,cc

/me/messages?$search="participants:[email protected]"
  • Related