【问题标题】:How to use exchangelib to get mail for non - inbox folders如何使用 exchangelib 获取非收件箱文件夹的邮件
【发布时间】:2018-07-17 06:41:11
【问题描述】:

我想获取非收件箱文件夹的邮件 - 我该怎么做?

我可以像这样获取收件箱文件夹的电子邮件:

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)

如何使用 Python 从ccc 文件夹中取出电子邮件?

【问题讨论】:

    标签: python email exchangelib


    【解决方案1】:

    查看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】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-25
        • 2018-06-13
        • 2018-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多