使用Python获取未读电子邮件

问题描述 投票:0回答:1

有什么方法可以从我的公司帐户中获取所有未读的电子邮件吗?我已尝试以下代码但没有成功。

from imbox import Imbox


host = 'outlook.office365.com'
email = '' My e-mail here
password = '' - My password here

with Imbox(host, username=email, password=password) as imbox:
    messages = imbox.messages(unread=True)

for uid, message in messages:
    print(f"De: {message.sent_from}")
    print(f"Assunto: {message.subject}")
    print(f"Corpo do Email: {message.body['plain']}")
python office365
1个回答
0
投票

您没有描述问题,但我看到一个错误:

所有代码都必须位于

with... as
内部,因为当它离开
with...as
时,它可能会关闭连接,并且您可能无法访问消息 - 所以您必须更改缩进

with Imbox(host, username=email, password=password) as imbox:
    messages = imbox.messages(unread=True)

    for uid, message in messages:
        print(f"De: {message.sent_from}")
        print(f"Assunto: {message.subject}")
        print(f"Corpo do Email: {message.body['plain']}")

或者你必须以正常方式创建它

imbox = Imbox(...)

imbox = Imbox(host, username=email, password=password)

messages = imbox.messages(unread=True)

for uid, message in messages:
    print(f"De: {message.sent_from}")
    print(f"Assunto: {message.subject}")
    print(f"Corpo do Email: {message.body['plain']}")

imbox.close()
© www.soinside.com 2019 - 2024. All rights reserved.