Fetch List of messages using message id of 500 mails using java

93 views Asked by At

As per documentation we have two ways to fetch mails using Graph api.

  1. Using $filter with .top(int ) we can fetch list of messages.
  2. Using message id we can fetch a single mail.

I have 50000 mails in my folder. I am using pageSize=500.Its working fine. Now I also want to move/delete/change status of a mail based on some criteria.

After performing update operation(move/delete/change status) my next page is getting disturbed. Its skipping few mail because in next page top is used.

For example : I have 25 mails in folder, I am using $filter with page size=3, top(3) so that only 15 mails are eligible for filtration. Now in each page I am getting 3 mails.

Now when I apply any update operation , after that I am not getting expected mails in next page. Some mails are getting skipped.

Any idea how we can over come this?

MY APPROACH: Fetch all message ids and keeping it in my cache. From next time onwards, I am using these message ids and fetching mails one by one.

Now the issue is that fetching 500 mails one by one is hitting performance badly. Do we have any api where we can send list of message ids and get List of messages?

Or

Do we have any settings such that next page fetching query will start from exactly next item.

2

There are 2 answers

1
Barry Gervin On

Might be better to scan your list first (cache up your updates), and then batch the updates together after you've iterated the list. Aside from fixing your problem, probably more efficient as well.

Barry Gervin

0
AlainC On

you get a list of IDs with for example

final MessageCollectionPage messagePage = graphClient.users().byId(bal).messages().buildRequest().top(10).select("subject,sentDateTime,internetMessageHeaders,internetMessageId").expand("singleValueExtendedProperties($filter=Id eq 'LONG 0x0E08')").get();

then you get and process the mails contents in parallel :

for (Message msg : messagePage.getCurrentPage()) {
graphClient.users().byId(bal).messages(msg.id).content().buildRequest().getAsync()
    .thenApply(inputStream -> {
        (...process...)
    })
    .thenApply(o -> {
        //delete mail
        graphClient.users().byId(bal).messages(msg.id).buildRequest().delete();
});
}

It should be faster.