2017-04-19 20 views
0

connect-to-exchange-mailbox-with-python/3072491 ....私はExchange Onlineに接続して添付ファイルをダウンロードし、Windows上でメールを読むために(Pythonとexchangelibライブラリを使用して)次のリンクを尊重しました。今CentOSで同じタスクを実行したいのですが、手動でexchangelibライブラリをダウンロードしてインストールするとします。 私はexchangelibをインポートしようとするたびに、それはのようなエラーがスローされます。問題がある可能性がありますどのようなメールを読んでMicrosoft Exchange Serverから添付ファイルをダウンロードしてください

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='[email protected]', 
    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で私を助けてください。

+0

のはなぜですこれはタイトルのcentos/centosでタグ付けされていますか?それはcentos特有ではないようです。 –

答えて

0

exchangelibは、さまざまなサードパーティのパッケージに依存しているため、パッケージをダウンロードしてインポートするだけでは使用できません。あなたは自動的にこれらのパッケージがインストールされて取得するpipを使用して、それをインストールする必要があります。

$ pip install exchangelib 
0

これは、あなたがすべてのメールを読んで、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?

関連する問題