以下是為了能夠滿足段落所需的長度而定義的無意義內文,請自行參酌編排。
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()