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

标签 python email exchangelib

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

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

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 文件夹中获取电子邮件?

最佳答案

查看最近版本的 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)

关于python - 如何使用 exchangelib 获取非收件箱文件夹的邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48655934/

相关文章:

python - 需要等效的函数来获取 Exchangelib Python 中的 Alias,就像 win32com 中的这个函数 (item.sender.getexchange().Alias)

python - exchangelib 和 pyinstaller - zoneinfo - tzdata - UTC 问题

python - Exchangelib:将电子邮件从收件箱移动到文件夹

python - 汇合卡夫卡: Consumer does not read from beginning for all partitions in a topic

python - 是否有任何 Python 环境支持编辑并继续?

python - 如何将 python "get()"用于比第一级字典键更深的键?

python - 在循环中导入模块

javascript - 发送的图像与山魈不显示

HTML 电子邮件 : How do I remove spacing between 2 tables? Gmail 特别

ruby - 在使用 ruby​​ 中的 "mail"gem 阅读电子邮件时需要帮助