在文章中顯示程式碼

以下是為了能夠滿足段落所需的長度而定義的無意義內文,請自行參酌編排。

import imaplib
import email

imap_server = "mail.asdfasdf.com"
email_address = "[email protected]"
pw = "asdasdasd"

imap = imaplib.IMAP4_SSL(imap_server)
imap.login(email_address, pw)

imap.select("Inbox")
_, msgnums = imap.search(None, "ALL")

for msgnum in msgnums[0].split():
    _, data = imap.fetch(msgnum, "(RFC822)")
    
    message = email.message_from_bytes(data[0][1])

    print(f"Message Number: {msgnum}")
    print(f"From: {message.get('From')}")
    print(f"To: {message.get('To')}")
    print(f"Date: {message.get('Date')}")
    print(f"Subject: {message.get('Subject')}")
    print("Content:")
    for part in message.walk():
        if part.get_content_type() == "text/plain":
            print(part.as_string())

imap.close()

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Scroll to Top