Last active
July 10, 2022 06:24
-
-
Save aspose-com-gists/971f68115475795758003fdd3ebc579d to your computer and use it in GitHub Desktop.
Read Emails in Python using POP3 or IMAP | https://blog.aspose.com/2021/06/04/read-emails-in-python-using-pop3-or-imap/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# connect to mail server using IMAP | |
client = ImapClient("imap.gmail.com", 993, "username", "password") | |
# select folder | |
client.select_folder("Inbox") | |
# loop through email messages and save them as .eml files | |
for msg in client.list_messages(): | |
print("Subject: " + msg.subject) | |
print("HtmlBody: " + msg.html_body) | |
print("TextBody: " + msg.body) | |
client.save_message(msg.unique_id, msg.unique_id + "_out.eml") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create POP3 client | |
client = Pop3Client("pop.gmail.com", 995, "username", "password") | |
# set security options | |
client.security_options = SecurityOptions.AUTO | |
# get message count | |
messageCount = client.get_message_count() | |
print("Total messages: " + str(messageCount)) | |
# create an instance of the MailMessage class to read message | |
for i in range(0,messageCount): | |
message = client.fetch_message(i+1) | |
print("From:" + str(message.from_address)) | |
print("Subject:" + message.subject) | |
print(message.html_body) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read the complete article: https://blog.aspose.com/2021/06/04/read-emails-in-python-using-pop3-or-imap/