Manipulate Folders (3)
The PST files are used to store information that pertains to the e-mail folders, addresses, contact information, email messages and other data that is saved within Outlook and Exchange programs. Spire.Email supports to read PST files and get the folder information such as folder name, message count and unread message count.
Step 1: Load a PST file from disk into an instance of OutlookFile class.
OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst");
Step 2: Get the folders collection.
OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders();
Step 3: Traverse the collection and get the folder information of each element in the collection.
foreach (OutlookFolder folder in folderCollection)
{
Console.WriteLine("Folder: " + folder.Name);
Console.WriteLine("Total items: " + folder.ItemCount);
Console.WriteLine("Total unread items: " + folder.UnreadItemCount);
Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders)?"Yes":"No");
Console.WriteLine("------------------Next Folder--------------------");
}
Output:

Full Code:
using Spire.Email;
using Spire.Email.Outlook;
using System;
namespace GetFolderInformation
{
class Program
{
static void Main(string[] args)
{
OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst");
OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders();
foreach (OutlookFolder folder in folderCollection)
{
Console.WriteLine("Folder: " + folder.Name);
Console.WriteLine("Total items: " + folder.ItemCount);
Console.WriteLine("Total unread items: " + folder.UnreadItemCount);
Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders) ? "Yes" : "No");
Console.WriteLine("------------------Next Folder--------------------");
}
Console.WriteLine("Completed");
}
}
}
Imports Spire.Email
Imports Spire.Email.Outlook
Namespace GetFolderInformation
Class Program
Private Shared Sub Main(args As String())
Dim olf As New OutlookFile("C:\Users\jack\Documents\Outlook Files\Sample.pst")
Dim folderCollection As OutlookFolderCollection = olf.RootOutlookFolder.GetSubFolders()
For Each folder As OutlookFolder In folderCollection
Console.WriteLine("Folder: " + folder.Name)
Console.WriteLine("Total items: " + folder.ItemCount)
Console.WriteLine("Total unread items: " + folder.UnreadItemCount)
Console.WriteLine("Whether this folder has subfolders: {0}", If((folder.HasSubFolders), "Yes", "No"))
Console.WriteLine("------------------Next Folder--------------------")
Next
Console.WriteLine("Completed")
End Sub
End Class
End Namespace
Spire.Email supports to manage folder subscriptions by using ImapClient.Subscribe and ImapClient.Unsubscribe method.
The following example shows how to subscribe to a folder and unsubscribe from a folder using Spire.Email component.
Detail steps:
Step 1: Create an ImapClient instance.
ImapClient imap = new ImapClient();
Step 2: Set host, port, authentication and connection protocol.
imap.Host = "outlook.office365.com"; imap.Port = 143; imap.Username = "LeonDavisLD@outlook.com"; imap.Password = "password"; imap.ConnectionProtocols = ConnectionProtocols.Ssl;
Step 3: Connect the imap server.
imap.Connect();
Step 4: Subscribe folder using its name.
imap.Subscribe("Folder1");
Step 5: Unsubscribe folder.
imap.Unsubscribe("Folder2");
Screenshot:

Full code:
using Spire.Email;
using Spire.Email.IMap;
namespace SubscribAndUnsubscribeFolders
{
class Program
{
static void Main(string[] args)
{
//Create an ImapClient instance
ImapClient imap = new ImapClient();
//Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;
//Connect the imap server
imap.Connect();
//subscribe folder using its name
imap.Subscribe("Folder1");
//Unsubscribe folder
imap.Unsubscribe("Folder2");
}
}
}
Imports Spire.Email
Imports Spire.Email.IMap
Namespace SubscribAndUnsubscribeFolders
Class Program
Private Shared Sub Main(args As String())
'Create an ImapClient instance
Dim imap As New ImapClient()
'Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com"
imap.Port = 143
imap.Username = "LeonDavisLD@outlook.com"
imap.Password = "password"
imap.ConnectionProtocols = ConnectionProtocols.Ssl
'Connect the imap server
imap.Connect()
'subscribe folder using its name
imap.Subscribe("Folder1")
'Unsubscribe folder
imap.Unsubscribe("Folder2")
End Sub
End Class
End Namespace
Following code snippets demonstrate how to create, rename and delete a folder on mail server by using ImapClient class in Spire.Email.
Create a mail folder
using Spire.Email;
using Spire.Email.IMap;
using System;
namespace CreateMailFolder
{
class Program
{
static void Main(string[] args)
{
//create an imapclient with username, password and host
ImapClient client = new ImapClient();
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();
//create a folder named ‘e-iceblue’
client.CreateFolder("e-iceblue");
Console.WriteLine("Done!");
}
}
}

Rename a mail folder
using Spire.Email;
using Spire.Email.IMap;
using System;
namespace RenameMailFolder
{
class Program
{
static void Main(string[] args)
{
//create an imapclient with username, password and host
ImapClient client = new ImapClient();
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();
//rename an existing folder with a new name
client.RenameFolder("e-iceblue", "E-ICEBLUE");
Console.WriteLine("Done!");
}
}

Delete a mail folder
using Spire.Email;
using Spire.Email.IMap;
using System;
namespace DeleteMailFolder
{
class Program
{
static void Main(string[] args)
{
//create an imapclient with username, password and host
ImapClient client = new ImapClient();
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();
//delete an existing folder
client.DeleteFolder("E-ICEBLUE");
Console.WriteLine("Done!");
}
}
}
