Folders

IMAP holds messages in a folder-like structure, so a folder needs to be selected before any message operations can be executed. Folders, called labels by some providers, can be created, renamed, and removed by users.

The example shows how to list all the folders on a server using the GemBox.Email component, and how to create, rename, and remove a custom folder.

Screenshot

Folders Screenshot

See the full code below.

1using GemBox.Email;
2using GemBox.Email.Imap;
3using GemBox.Email.Mime;
4using GemBox.Email.Security;
5using System;
6using System.Collections.Generic;
7using System.Globalization;
8using System.Net.Security;
9using System.Security.Cryptography.X509Certificates;
10
11class Sample
12{
13    [STAThread]
14    static void Main(string[] args)
15    {
16        // If using Professional version, put your serial key below.
17        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
18
19        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
20        {
21            imap.Connect();
22            Console.WriteLine("Connected.");
23
24            imap.Authenticate("<USERNAME>", "<PASSWORD>");
25            Console.WriteLine("Authenticated.");
26
27            // List all folders on the server
28            IList<ImapFolderInfo> folders = imap.ListFolders();
29
30            Console.WriteLine("Listing folders...");
31
32            foreach (ImapFolderInfo info in folders)
33                Console.WriteLine(" {0,18}: {1}", info.Name, string.Join(", ", info.Flags));
34
35            // Create new folder and list all folders again
36            imap.CreateFolder("GemBox");
37
38            Console.WriteLine("Listing folders again...");
39
40            folders = imap.ListFolders();
41            foreach (ImapFolderInfo info in folders)
42                Console.WriteLine(" {0,18}: {1}", info.Name, string.Join(", ", info.Flags));
43
44            // Delete newly created folder and repeat listing
45            imap.DeleteFolder("GemBox");
46
47            Console.WriteLine("Final folder listing...");
48
49            folders = imap.ListFolders();
50            foreach (ImapFolderInfo info in folders)
51                Console.WriteLine(" {0,18}: {1}", info.Name, string.Join(", ", info.Flags));
52        }
53    }
54}
1Imports System.Globalization
2Imports System.Net.Security
3Imports System.Security.Cryptography.X509Certificates
4Imports GemBox.Email
5Imports GemBox.Email.Imap
6Imports GemBox.Email.Mime
7
8Module Samples
9
10    Sub Main()
11
12        ' If using Professional version, put your serial key below.
13        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
14
15        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")
16
17            imap.Connect()
18            Console.WriteLine("Connected.")
19
20            imap.Authenticate("<USERNAME>", "<PASSWORD>")
21            Console.WriteLine("Authenticated.")
22
23            ' List all folders on the server
24            Dim folders As IList(Of ImapFolderInfo) = imap.ListFolders()
25
26            Console.WriteLine("Listing folders...")
27
28            For Each info As ImapFolderInfo In folders
29                Console.WriteLine(" {0,18}: {1}", info.Name, String.Join(", ", info.Flags))
30            Next
31
32            ' Create new folder and list all folders again
33            imap.CreateFolder("GemBox")
34
35            Console.WriteLine("Listing folders again...")
36
37            folders = imap.ListFolders()
38            For Each info As ImapFolderInfo In folders
39                Console.WriteLine(" {0,18}: {1}", info.Name, String.Join(", ", info.Flags))
40            Next
41
42            ' Delete newly created folder and repeat listing
43            imap.DeleteFolder("GemBox")
44
45            Console.WriteLine("Final folder listing...")
46
47            folders = imap.ListFolders()
48            For Each info As ImapFolderInfo In folders
49                Console.WriteLine(" {0,18}: {1}", info.Name, String.Join(", ", info.Flags))
50            Next
51
52        End Using
53
54    End Sub
55
56End Module

Check next sample or find out more about GemBox.Email and GemBox Software.