Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active December 17, 2021 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/9132c041d110e75d026870a96a08b189 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/9132c041d110e75d026870a96a08b189 to your computer and use it in GitHub Desktop.
Read Emails from Email Server using IMAP in Python
from aspose.email import SecurityOptions, ImapClient
# create an instance of ImapClient and provide host, port, username and password
client = ImapClient("imap.domain.com", 993, "user@domain.com", "password")
# set security options
client.security_options = SecurityOptions.SSLIMPLICIT
# use ImapClient object to access mailbox
from aspose.email import ImapClient
# create an instance of ImapClient and provide host, port, username and password
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
# get list of folders
folderInfoColl = client.list_folders()
# iterate through the collection to get folders' info one by one
for folderInfo in folderInfoColl:
print("Folder name is " + folderInfo.name)
folderExtInfo = client.get_folder_info(folderInfo.name)
print("New message count: " + str(folderExtInfo.new_message_count))
print("Is it readonly? " + str(folderExtInfo.read_only))
print("Total number of messages " + str(folderExtInfo.total_message_count))
from aspose.email import ImapClient
# create an instance of ImapClient and provide host, port, username and password
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
# select folder
client.select_folder("Inbox")
# read each message in collection
for msg in client.list_messages():
print( "From: '{}', MIME Id: {}".format(msg.from_address, msg.message_id) )
# save message on disk
client.save_message(msg.unique_id, msg.unique_id + "_out.eml")
from aspose.email import ImapClient
# create an instance of ImapClient and provide host, port, username and password
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
# select the folder from mailbox
client.select_folder("Inbox")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment