【问题标题】:Read emails and download attachment from Microsoft Exchange Server从 Microsoft Exchange Server 阅读电子邮件和下载附件
【发布时间】:2017-09-15 10:39:00
【问题描述】:

connect-to-exchange-mailbox-with-python/3072491....我参考了以下链接以连接到 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

可能是什么问题?

我的主要目标是阅读电子邮件并下载它们。没有可用的 imap/pop3 服务器地址。 exchangelib 有替代品吗?

from exchangelib import DELEGATE, Account, Credentials

credentials = Credentials(
    username='MYWINDOMAIN\\myusername', 
    password='topsecret'
)
account = Account(
    primary_smtp_address='john@example.com', 
    credentials=credentials, 
    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)

我在 Windows 中使用过此代码。帮我搞定 Linux。

【问题讨论】:

  • 为什么在标题中标有centos / centos?它似乎不是特定于 centos 的。

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


【解决方案1】:

exchangelib 依赖于各种 3rd 方包,所以你不能只下载和导入包。您需要使用pip 安装它才能自动安装这些软件包:

$ pip install exchangelib

【讨论】:

    【解决方案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?

    【讨论】:

    • 仅当 imap 在您访问的特定 Exchange 邮箱上处于活动状态时才有效,对吗? ,如果是,有什么方法可以检查 imap 是否处于活动状态?如果没有,有什么解决方法?
    猜你喜欢
    • 2023-03-15
    • 2013-12-01
    • 2011-08-02
    • 1970-01-01
    • 2013-11-12
    • 2014-05-10
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多