Manage, Move, and Organize IMAP Folders
Folder Operations
Get Folder Information
Getting information about folders from an IMAP server is very easy with Aspose.Email. Call the ListFolders() method of the ImapClient class. 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-for-.NET | |
// Get all folders in the currently subscribed folder | |
ImapFolderInfoCollection folderInfoColl = client.ListFolders(); | |
// Iterate through the collection to get folder info one by one | |
foreach (ImapFolderInfo folderInfo in folderInfoColl) | |
{ | |
// Folder name and get New messages in the folder | |
Console.WriteLine("Folder name is " + folderInfo.Name); | |
ImapFolderInfo folderExtInfo = client.GetFolderInfo(folderInfo.Name); | |
Console.WriteLine("New message count: " + folderExtInfo.NewMessageCount); | |
Console.WriteLine("Is it readonly? " + folderExtInfo.ReadOnly); | |
Console.WriteLine("Total number of messages " + folderExtInfo.TotalMessageCount); | |
} |
Delete and Rename Folders
A folder on an IMAP server can be deleted or renamed in a single line with Aspose.Email:
- The DeleteFolder() method accepts the folder name as a parameter.
- For the RenameFolder() method, you need to pass the current folder name and new folder name. The following code snippet shows you how to remove a folder from an IMAP server, and how to rename a folder. Each operation is performed with one line of code.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Delete a folder and Rename a folder | |
client.DeleteFolder("foldername"); | |
client.RenameFolder("foldername", "newfoldername"); |
Working with Special-Use Mailbox Folders
Some IMAP message stores include special-use mailboxes, such as those used to hold draft messages or sent messages. Many mail clients allow users to specify where the draft or sent messages should be put, but configuring them requires that the user knows which mailboxes the server has set aside for these purposes. Aspose.Email can identify these special-use mailboxes by using the ImapMailboxInfo class to make it easier to work with them. The following code sample demonstrates how to access these special-use mailboxes by using the ImapMailboxInfo class.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
ImapClient imapClient = new ImapClient(); | |
imapClient.Host = "<HOST>"; | |
imapClient.Port = 993; | |
imapClient.Username = "<USERNAME>"; | |
imapClient.Password = "<PASSWORD>"; | |
imapClient.SupportedEncryption = EncryptionProtocols.Tls; | |
imapClient.SecurityOptions = SecurityOptions.SSLImplicit; | |
ImapMailboxInfo mailboxInfo = imapClient.MailboxInfo; | |
Console.WriteLine(mailboxInfo.Inbox); | |
Console.WriteLine(mailboxInfo.DraftMessages); | |
Console.WriteLine(mailboxInfo.JunkMessages); | |
Console.WriteLine(mailboxInfo.SentMessages); | |
Console.WriteLine(mailboxInfo.Trash); |
Message Operations within Folders
Add a New Message to a Folder
You can add a new message to a folder using the MailMessage and ImapClient classes. 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-for-.NET | |
// Create a message | |
MailMessage msg = new MailMessage("user@domain1.com", "user@domain2.com", "subject", "message"); | |
// Subscribe to the Inbox folder and Append the newly created message | |
client.SelectFolder(ImapFolderInfo.InBox); | |
client.SubscribeFolder(client.CurrentFolder.Name); | |
client.AppendMessage(client.CurrentFolder.Name, msg); |
Add Multiple Messages with MultiConnection
You can add multiple messages by using the AppendMessages method provided by the ImapClient classes. The AppendMessages method accepts a list of MailMessage and adds it to the current folder if the folder is not provided as a parameter. ImapClient also supports MultiConnection mode for heavy loaded operations. The following code snippet shows you how to add multiple messages by using the MultiConnection mode.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
ImapClient imapClient = new ImapClient(); | |
imapClient.Host = "<HOST>"; | |
imapClient.Port = 993; | |
imapClient.Username = "<USERNAME>"; | |
imapClient.Password = "<PASSWORD>"; | |
imapClient.SupportedEncryption = EncryptionProtocols.Tls; | |
imapClient.SecurityOptions = SecurityOptions.SSLImplicit; | |
List<MailMessage> messages = new List<MailMessage>(); | |
for (int i = 0; i < 20; i++) | |
{ | |
MailMessage message = new MailMessage( | |
"<EMAIL ADDRESS>", | |
"<EMAIL ADDRESS>", | |
"Test Message - " + Guid.NewGuid().ToString(), | |
"IMAP Group Append with MultiConnection"); | |
messages.Add(message); | |
} | |
imapClient.ConnectionsQuantity = 5; | |
imapClient.UseMultiConnection = MultiConnectionMode.Enable; | |
imapClient.AppendMessages(messages); |
Move Messages between Folders
Aspose.Email for .NET allows moving message from one mailbox folder to another using the ImapClient API. The MoveMessage method uses the message 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-for-.NET | |
///<summary> | |
/// This example shows how to move a message from one folder of a mailbox to another one using the ImapClient API of Aspose.Email for .NET | |
/// Available from Aspose.Email for .NET 6.4.0 onwards | |
/// -------------- Available API Overload Members -------------- | |
/// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName, bool commitDeletions) | |
/// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName, bool commitDeletions) | |
/// Void ImapClient.MoveMessage(int sequenceNumber, string folderName, bool commitDeletions) | |
/// Void ImapClient.MoveMessage(string uniqueId, string folderName, bool commitDeletions) | |
/// Void ImapClient.MoveMessage(IConnection iConnection, int sequenceNumber, string folderName) | |
/// Void ImapClient.MoveMessage(IConnection iConnection, string uniqueId, string folderName) | |
/// Void ImapClient.MoveMessage(int sequenceNumber, string folderName) | |
/// Void ImapClient.MoveMessage(string uniqueId, string folderName) | |
///</summary> | |
using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password")) | |
{ | |
string folderName = "EMAILNET-35151"; | |
if (!client.ExistFolder(folderName)) | |
client.CreateFolder(folderName); | |
try | |
{ | |
MailMessage message = new MailMessage( | |
"from@domain.com", | |
"to@domain.com", | |
"EMAILNET-35151 - " + Guid.NewGuid(), | |
"EMAILNET-35151 ImapClient: Provide option to Move Message"); | |
client.SelectFolder(ImapFolderInfo.InBox); | |
// Append the new message to Inbox folder | |
string uniqueId = client.AppendMessage(ImapFolderInfo.InBox, message); | |
ImapMessageInfoCollection messageInfoCol1 = client.ListMessages(); | |
Console.WriteLine(messageInfoCol1.Count); | |
// Now move the message to the folder EMAILNET-35151 | |
client.MoveMessage(uniqueId, folderName); | |
client.CommitDeletes(); | |
// Verify that the message was moved to the new folder | |
client.SelectFolder(folderName); | |
messageInfoCol1 = client.ListMessages(); | |
Console.WriteLine(messageInfoCol1.Count); | |
// Verify that the message was moved from the Inbox | |
client.SelectFolder(ImapFolderInfo.InBox); | |
messageInfoCol1 = client.ListMessages(); | |
Console.WriteLine(messageInfoCol1.Count); | |
} | |
finally | |
{ | |
try { client.DeleteFolder(folderName); } | |
catch { } | |
} | |
} |
Copy Messages between Folders
Aspose.Email API provides the capability to copy message from one mailbox folder to another. It allows copying 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-for-.NET | |
using (ImapClient client = new ImapClient("exchange.aspose.com", "username", "password")) | |
{ | |
// Create the destination folder | |
string folderName = "EMAILNET-35242"; | |
if (!client.ExistFolder(folderName)) | |
client.CreateFolder(folderName); | |
try | |
{ | |
// Append a couple of messages to the server | |
MailMessage message1 = new MailMessage( | |
"asposeemail.test3@aspose.com", | |
"asposeemail.test3@aspose.com", | |
"EMAILNET-35242 - " + Guid.NewGuid(), | |
"EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation."); | |
string uniqueId1 = client.AppendMessage(message1); | |
MailMessage message2 = new MailMessage( | |
"asposeemail.test3@aspose.com", | |
"asposeemail.test3@aspose.com", | |
"EMAILNET-35242 - " + Guid.NewGuid(), | |
"EMAILNET-35242 Improvement of copy method.Add ability to 'copy' multiple messages per invocation."); | |
string uniqueId2 = client.AppendMessage(message2); | |
// Verify that the messages have been added to the mailbox | |
client.SelectFolder(ImapFolderInfo.InBox); | |
ImapMessageInfoCollection msgsColl = client.ListMessages(); | |
foreach (ImapMessageInfo msgInfo in msgsColl) | |
Console.WriteLine(msgInfo.Subject); | |
// Copy multiple messages using the CopyMessages command and Verify that messages are copied to the destination folder | |
client.CopyMessages(new[] { uniqueId1, uniqueId2 }, folderName); | |
client.SelectFolder(folderName); | |
msgsColl = client.ListMessages(); | |
foreach (ImapMessageInfo msgInfo in msgsColl) | |
Console.WriteLine(msgInfo.Subject); | |
} | |
finally | |
{ | |
try | |
{ | |
client.DeleteFolder(folderName); | |
} | |
catch { } | |
} | |
} |