Tony Frank
Tony Frank

Reputation: 308

Microsoft GRAPH API to get total number of mails in my inbox

I am writing to script to check my mail and download the attachments. What I am trying to do is to check my Total inbox mail count every 5mins and find if there is a new mail and download its attachments. I am following this python tutorial. Below is my function to get the total number of mails in my inbox

def get_all_msg(self):
    endpoint = '/me/mailFolders/inbox/messages'
    request_url = f'{endpoint}?$count=true'

    inbox_response = self.user_client.get(request_url)
    return inbox_response.json()['@odata.count']

This returns 1520 but the total number of mails in my inbox is around 250 But if there is a new mail the function returns 1521 (gets incremented by 1 expected)

Not sure if this is the correct approach

Any input is much appreciated

Upvotes: 0

Views: 837

Answers (1)

user2250152
user2250152

Reputation: 20768

I would prefer to use message delta to get a set of messages that have been added, deleted, or updated in a specified folder. It's better approach then check mails count.

GET /me/mailFolders/inbox/messages/delta
GET /me/mailfolders/inbox/messages/delta?changeType=created

It will return a message collection.

Upvotes: 1

Related Questions