Search code examples
pythonexchangewebservicesexchangelib

Using python exchangelib, how to get total size of messages in mail folder


I can iterate over all the mail folder using for item in account.root.get_folders() and if item.__class__ == exchangelib.folders.Messages: but how can I get the amount of space used by messages in that folder?

I've read online from that foldersize is a EWS extended property type, but how do I get that via exchangelib? (FYI, the property is PropertyTag: 0x0e08; PropertyType: Integer)


Solution

  • exchangelib doesn't support extended properties on folders yet. EWS does expose a size attribute on items that could easily be added to exchangelib (feel free to open an issue:-)), which would allow you to do something like this:

    sum(some_folder.all().values_list('size', flat=True))
    

    Update: Here is example code of using this to get folder size of all email (class folder.Message) folders:

    for folder in account.root.find_folders():
      if folder.__class__ != exchangelib.folders.Messages:
          continue
      fsum = sum(folder.all().values_list('size', flat=True))
      print('{0:>40s} {1:12,d}'.format(folder.name.encode('utf-8'), fsum))