Threading is done entirely on the server side (if your IMAP server supports THREAD extension)
Following sample shows how to use Imap.Thread method to thread all emails available in the mailbox.
- First it checks if THREAD extension is available
- It downloads envelopes of all email messages to get messages subjects and some basic data.
- Finally recursive ShowThread method is used - it displays message UID and subject
Here's the sample:
using (Imap imap = new Imap())
{
imap.Connect("imap.example.org"); // or ConnectSSL
imap.UseBestLogin("user", "password");
ThreadMethod method = ThreadMethod.ChooseBest(
imap.SupportedThreadMethods());
if (method == null)
throw new Exception("This server doesn't support threading.");
imap.SelectInbox();
List<MessageThread> threads = imap.Thread(method).Where(Expression.All());
List<Envelope> envelopes = imap.GetEnvelopeByUID(
imap.Search().Where(Expression.All()));
foreach (MessageThread thread in threads)
{
ShowThread(0, thread, envelopes);
}
imap.Close();
}
public void ShowThread(int level, MessageThread thread,
List<Envelope> envelopes)
{
string indent = new string(' ', level*4);
string subject = envelopes.Find(x => x.UID == thread.UID).Subject;
Console.WriteLine("{0}{1}: {2}", indent, thread.UID, subject);
foreach (MessageThread child in thread.Children)
{
ShowThread(level + 1, child, envelopes);
}
}