0

connect-to-exchange-mailbox-with-python/3072491 .... Ich habe den folgenden Link, um eine Verbindung zu Exchange Online und Download von Anlagen und Lesen von Mails auf Windows (mit Python und ExchangeLib-Bibliothek) verwiesen. Jetzt möchte ich die gleiche Aufgabe auf CentOS durchführen, aber wenn ich die exchangelib Bibliothek manuell herunterlade und es installiere. Immer wenn ich versuche exchangelib zu importieren, wirft es einen Fehler wie:E-Mails lesen und Anhang von Microsoft Exchange Server herunterladen

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 

Was das Problem sein könnte?

Mein Hauptziel ist es, E-Mails zu lesen und herunterzuladen. Es ist keine IMAP/POP3-Serveradresse verfügbar. Gibt es eine Alternative zu 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) 

Ich habe diesen Code in Windows verwendet. Hilf mir mit Linux.

+0

Warum ist Dies markiert mit Centos/Centos im Titel? Es scheint nicht Centos spezifisch zu sein. –

Antwort

0

exchangelib hängt von verschiedenen 3rd-Party-Paketen ab, so dass Sie das Paket nicht einfach herunterladen und importieren können. Sie müssen es installieren pip verwenden diese Pakete automatisch installiert zu bekommen:

$ pip install exchangelib 
0

Dies ist, wie Sie alle E-Mails lesen und speichern Sie alle Anhänge mit 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) 

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

Verwandte Themen