Python 阅读电子邮件并从Microsoft Exchange Server下载附件

Python 阅读电子邮件并从Microsoft Exchange Server下载附件,python,smtp,exchange-server,email-attachments,exchangelib,Python,Smtp,Exchange Server,Email Attachments,Exchangelib,..我参考了以下链接以连接到Exchange Online并在windows上下载附件和阅读邮件(使用Python和exchangelib库)。现在,我想在CentOS上完成同样的任务,但是当我手动下载exchangelib库并安装它时。 每当我尝试导入exchangelib时,它都会抛出如下错误: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "exchan

..我参考了以下链接以连接到Exchange Online并在windows上下载附件和阅读邮件(使用Python和exchangelib库)。现在,我想在CentOS上完成同样的任务,但是当我手动下载
exchangelib
库并安装它时。 每当我尝试导入exchangelib时,它都会抛出如下错误:

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

我在Windows中使用过此代码。帮助我了解Linux。

exchangelib
依赖于各种第三方软件包,因此您不能只下载和导入软件包。您需要使用
pip
安装它,以便自动安装这些软件包:

$ pip install exchangelib

exchangelib
依赖于各种第三方软件包,因此您不能只下载和导入软件包。您需要使用
pip
安装它,以便自动安装这些软件包:

$ pip install exchangelib

这就是使用
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)

相关:

这是使用
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)

相关:

为什么在标题中用centos/centos标记?它似乎不是特定于centos的。为什么在标题中标记为centos/centos?这似乎不是针对centos的。