2

I am maintaining an application using exchangelib. I need to be able to move emails to different folders, which is perfectly possible using exchangelib. However, I should give the possibility to my clients to do the same manually. What I want to be able to do is to see what email is moved to what folder later. So I need a tracking id for emails. So may questions are:

  1. Does the message_id field serve as this unique identifier?
  2. It seems it is not possible to fetch emails in bulk using message_id. What is the best practice for this? I can fetch emails using item_id and changekey, by calling account.fetch however, they change as the user moves email around different folders, while the message_id does not.
Hamed
  • 474
  • 5
  • 17

1 Answers1

2

Re. 1, it seems you already found out that message_id is left untouched when an item is moved. I can't find any hard documentation from EWS regarding this fact, so make sure to do extensive testing.

Re. 2, it's true that you can only use account.fetch with item_id values. However, there are some things to note:

  1. When you call item.move(), item.item_id and item.changekey attributes are updated to the new location, which you could store for a future bulk operation. See https://github.com/ecederstrand/exchangelib/blob/3a1def29951d26c8a1b7021c7582c3d118181140/exchangelib/items.py#L365
  2. You can still fetch in bulk by message_id by first translating the message_id to an item_id. This does a FindItem call and then a GetItem call:

item_ids = account.inbox.filter(message_id__in=<your_message_ids>) \
    .values_list('item_id', 'changekey')
bulk_items = account.fetch(item_ids)

You probably want to chunk <your_message_ids> items so the __in filter does not get extremely large.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
  • Thanks a lot for you answers. That seems to solve the problem. Just a related question. Is there any possibility to force `filter()` function, search recursively inside folders? By default it just gos one level deep. – Hamed May 01 '18 at 09:59
  • 1
    Yes. The search API also works on collections of folders, e.g. `account.inbox.children.filter()` or `account.inbox.root.glob('foo*').filter()`. See the bottom of the https://github.com/ecederstrand/exchangelib#searching section. – Erik Cederstrand May 01 '18 at 10:38
  • @ErikCederstrand hello Erik.. How to get all mail property names list? like `importance`, `hasattachments` etc.. do we get a complete list of mail property names from exchangelib? – sjd Aug 04 '20 at 07:53
  • The easiest method is probably `for f in Message.FIELDS: print(f.name)`. See also https://ecederstrand.github.io/exchangelib/exchangelib/items/message.html – Erik Cederstrand Aug 04 '20 at 08:10