Minimales Python IMAP über SSL Beispiel
English
Deutsch
Note: Consider using IMAP with TLS instead of IMAP over SSL. See Minimal Python IMAP over TLS example.
Dieser Beispielcode wird sich mit Port 993 (IMAP über SSL) beim Server anmelden, die Postfächer auflisten und sich sofort abmelden.
imap_ssl_example.py
#!/usr/bin/env python3
import imaplib
server = imaplib.IMAP4_SSL('imap.mydomain.com')
server.login('email@mydomain.com', 'password')
# Liste der Postfächer auf dem Server ausgeben
code, mailboxes = server.list()
for mailbox in mailboxes:
print(mailbox.decode("utf-8"))
# Postfach auswählen
server.select("INBOX")
# Bereinigung
server.close()Denke daran zu ersetzen:
imap.mydomain.comwith the domain name or IP address of your IMAP serveremail@mydomain.comby the email address you want to login withpasswordby the password you want to login with
Wenn du dieses Skript ausführst, könnte eine erfolgreiche Ausgabe so aussehen:
imap_example_output.txt
(\HasChildren) "." INBOX
(\HasNoChildren) "." INBOX.Spam
(\HasNoChildren) "." INBOX.Drafts
(\HasNoChildren) "." INBOX.Sent
(\HasNoChildren) "." INBOX.TrashWenn deine Anmeldedaten nicht funktionieren, wirst du eine Fehlermeldung wie diese sehen:
imap_example_traceback.txt
Traceback (most recent call last):
File "./imaptest.py", line 5, in <module>
server.login('email@domain.com', 'mypassword')
File "/usr/lib/python3.6/imaplib.py", line 598, in login
raise self.error(dat[-1])
imaplib.error: b'[AUTHENTICATIONFAILED] Authentication failed.'Note that in order to be able to server.close() the connection, it’s required that you server.select() a mailbox first ; this is why we can’t just omit the server.select("INBOX") line even though we don’t actually do anything with the mailbox. See this post for a more concise example on this behaviour.
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow