Working with Folders on IMAP Server

Getting Folders Information

Getting information about folders from an IMAP server is very easy with Aspose.Email. Call the Aspose.Email.Imap namespace’s ListFolders() method. It returns an object of the ImapFolderInfoCollection type. Iterate through this collection and get information about individual folders in a loop. The method is overloaded. You can pass a folder name as a parameter to get a list of subfolders. The following code snippet shows you how to get folder information from an IMAP server using Aspose.Email using the method described in the information.

For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
folderInfoColl = client.list_folders();
#Iterate through the collection to get folder 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));

Deleting and Renaming Folders

The following methods of the ImapClient class related to managing folders on an email server using IMAP can be easily implemented into your project with Aspose.Email:

  • delete_folder method - permanently removes the folder and all of the messages contained within it.
  • rename_folder method - changes the name of the folder without altering the contents within it.

The code snippet below shows how to delete or rename folders on the IMAP server programmatically:

# Delete a folder and Rename a folder
client.delete_folder("foldername")
client.rename_folder("foldername", "newfoldername")

Adding a New Message in a Folder

You can add a new message to the folder using the MailMessage class and ImapClient classes. To First create a MailMessage object by providing the subject, to and from values. Then subscribe to a folder and add the message to it. The following code snippet shows you how to add a new Message in a folder.

For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
client.select_folder("Inbox")
msg = MailMessage("user@domain1.com", "user@domain2.com", "subject", "message")
client.subscribe_folder(client.current_folder.name)
client.append_message(client.current_folder.name, msg)

Establishing MultiConnection when Performing Batch Operations

Aspose.Email makes it possible to configure the client to establish multiple simultaneous connections to the IMAP server. It does not necessarily increase the performance, but it’s a reliable solution for concurrent operations. This is particularly useful if the client needs to perform multiple tasks at the same time, such as fetching different email folders, synchronizing large amounts of data, or processing multiple messages simultaneously.

The code snippet below shows how to establish multiple connections to the IMAP server while uploading a collection of email messages using the ‘append_messages’ method of the ImapClient class:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd", ae.clients.SecurityOptions.SSL_IMPLICIT)

client.connections_quantity = 5
client.use_multi_connection = ae.clients.MultiConnectionMode.ENABLE
client.append_messages(messages)

Move Messages to Another Mailbox Folder

Aspose.Email for .NET allows to move message from one mailbox folder to another using the ImapClient API. The MoveMessage method uses the message’s unique id and destination folder name for moving a message to the destination folder. The following code snippet shows you how to move messages to another mailbox folder.

For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
client.select_folder("Inbox")
folderName = "N1Renamed"
#Append a new Message to Inbox
msg = MailMessage("user@domain1.com", "user@domain2.com", "subject", "message")
msgId = client.append_message( msg)
#List messages from Inbox
msgsCollection = client.list_messages()
print("Total Messages in Inbox: " + str(len(msgsCollection)))
#Move message to another folder
client.move_message(msgId, folderName)
#List messages from Inbox
msgsCollection = client.list_messages()
print("Total Messages in Inbox: " + str(len(msgsCollection)))

Copy Messages to Another Mailbox Folder

Aspose.Email API provides the capability to copy message from one mailbox folder to another. It allows to copy a single as well as multiple messages using the CopyMessage and CopyMessages methods. The CopyMessages method provides the capability to copy multiple messages from source folder of a mailbox to the destination mailbox folder. The following code snippet shows you how to copy messages to another mailbox folder.

For complete examples and data files, please go to https://github.com/aspose-email/aspose-email-python-dotnet
with ImapClient("imap.gmail.com", 993, "username", "password") as client:
client.select_folder("Inbox")
folderName = "MovedMessagesToFolder"
#List messages from Inbox to verify that messages have been copied
msgsCollection = client.list_messages()
print("Total Messages in Inbox before appending: " + str(len(msgsCollection)))
#Append 2 new Messages to Inbox
msg = MailMessage("user@domain1.com", "user@domain2.com", "Message1TobeMoved", "message")
msgId1 = client.append_message(msg)
msg2 = MailMessage("user@domain1.com", "user@domain2.com", "Message1TobeMoved", "message")
msgId2 = client.append_message(msg2)
#List messages from Inbox to verify that messages have been copied
msgsCollection = client.list_messages()
print("Total Messages in Inbox after appending: " + str(len(msgsCollection)))
msgsIds = []
msgsIds.append(msgId1)
msgsIds.append(msgId2)
#Copy message to another folder
client.copy_messages(msgsIds, folderName)

Working with Special-Use Mailbox Folders

Special-use mailboxes are pre-designated folders within an email system used for specific types of messages, such as Sent, Drafts, Junk, Trash, and Archive. The Aspose.Email library allows for the access to these mailboxes by assigning attributes associated with their roles and purposes to the client. The clients then can automatically discover and present these folders accordingly without user intervention.

The following code snippet shows how to query information about the important special-use mailboxes (inbox, draft, junk, sent, and trash) using the properties of the ImapMailBoxInfo class, and print out this information:

import aspose.email as ae

client = ae.clients.imap.ImapClient("imap.domain.com", 993, "user@domain.com", "pwd", ae.clients.SecurityOptions.SSL_IMPLICIT)

mailboxInfo = client.mailbox_info
print(mailboxInfo.inbox)
print(mailboxInfo.draft_messages)
print(mailboxInfo.junk_messages)
print(mailboxInfo.sent_messages)
print(mailboxInfo.trash)