代码之家  ›  专栏  ›  技术社区  ›  王胖胖

如何使用exchangelib获取非收件箱文件夹的邮件

  •  4
  • 王胖胖  · 技术社区  · 8 年前

    我想获取非收件箱文件夹的邮件-我如何才能做到这一点?

    我可以收到收件箱文件夹的电子邮件,如下所示:

    from exchangelib import DELEGATE, Account, Credentials, EWSDateTime
    
    creds = Credentials(
        username='xxx.test.com\test',
        password='123456')
    account = Account(
        primary_smtp_address='test@test.com',
        credentials=creds,
        autodiscover=True,
        access_type=DELEGATE)
    
    # Print first 100 inbox messages in reverse order
    for item in account.inbox.all().order_by('-datetime_received')[:100]:
        # print(item.subject, item.body, item.attachments)
        print(item.subject)
    

    给:

    hahaha
    heiheihei
    pupupu
    bibibib
    ........
    

    当我收到文件夹时:

    from exchangelib.folders import Messages
    
    for f in account.folders[Messages]:
        print f
    
    Messages (aaa)
    Messages (bbb)
    Messages (ccc)
    

    我怎样才能从 ccc 使用Python的文件夹?

    2 回复  |  直到 8 年前
        1
  •  7
  •   Erik Cederstrand    8 年前

    查看最新版本中的文件夹导航选项 exchangelib : https://github.com/ecederstrand/exchangelib#folders

    可以按如下方式打印整个文件夹结构:

    print(account.root.tree())
    

    然后使用与相同的语法导航到特定文件夹 pathlib :

    some_other_folder = account.inbox / 'some_inbox_subfolder'
    # Or:
    some_other_folder = account.root / 'some' / 'other' / 'path'
    for item in some_other_folder.all().order_by('-datetime_received')[:100]:
        print(item.subject)
    
        2
  •  4
  •   Hugo Vares    7 年前

    您只能对收件箱子文件夹执行以下操作:

    for subfolder in account.inbox.children:
        for emailz in subfolder.all().only('subject','attachments','datetime_sent').order_by('-datetime_received'):
            #do your thing
    

    或所有根目录子文件夹:

    for subfolder in account.root.children:
        for emailz in subfolder.all().only('subject','attachments','datetime_sent').order_by('-datetime_received'):
            #do your thing