从Microsoft Exchange Server读取电子邮件和下载附件

时间:2017-04-19 09:30:07

标签: python smtp exchange-server email-attachments exchangelib

connect-to-exchange-mailbox-with-python/3072491 ....我已经通过以下链接来连接到Exchange Online并在Windows上下载附件和阅读邮件(使用Python和exchangelib库)。现在我想在CentOS上完成相同的任务,但是当我手动下载func getTomorrowAt(hour: Int, minutes: Int) -> Date { let calendar = Calendar.current var date = calendar.date(byAdding: .day, value: 1, to: Date()) // here I need to set the time to hour:minutes return date! } 库并安装它时。 每当我尝试导入exchangelib时,它都会抛出一个错误:

exchangelib

可能是什么问题?

我的主要目标是阅读电子邮件并下载它们。没有imap / pop3服务器地址可用。是否有替代Traceback (most recent call last): File "<stdin>", line 1, in <module> File "exchangelib/__init__.py", line 2, in <module> from .account import Account # noqa File "exchangelib/account.py", line 8, in <module> from cached_property import threaded_cached_property ImportError: No module named cached_property

exchangelib

我在Windows中使用过这段代码。帮我解决Linux问题。

2 个答案:

答案 0 :(得分:2)

这是您阅读所有电子邮件并使用exchangelib存储所有附件的方式:

from exchangelib import ServiceAccount, Configuration, Account, DELEGATE
import os

from config import cfg


credentials = ServiceAccount(username=cfg['imap_user'],
                             password=cfg['imap_password'])

config = Configuration(server=cfg['imap_server'], credentials=credentials)
account = Account(primary_smtp_address=cfg['smtp_address'], config=config,
                  autodiscover=False, access_type=DELEGATE)


unread = account.inbox.filter()   # returns all mails
for msg in unread:
    print(msg)
    print("attachments       ={}".format(msg.attachments))
    print("conversation_id   ={}".format(msg.conversation_id))
    print("last_modified_time={}".format(msg.last_modified_time))
    print("datetime_sent     ={}".format(msg.datetime_sent))
    print("sender            ={}".format(msg.sender))
    print("text_body={}".format(msg.text_body.encode('UTF-8')))
    print("#" * 80)
    for attachment in msg.attachments:
        fpath = os.path.join(cfg['download_folder'], attachment.name)
        with open(fpath, 'wb') as f:
            f.write(attachment.content)

相关:How can I send an email with an attachment with Python and Microsoft Exchange?

答案 1 :(得分:0)

exchangelib取决于各种第三方软件包,因此您无法下载和导入软件包。您需要使用pip安装它才能自动安装这些软件包:

$ pip install exchangelib