+1 vote

Hi Team,

I want to access the all the threads against a particular email. Suppose I have sent a message to say ten persons and they replied on that message. Now I would like to access all the replies along with the original one that I have sent earlier.

Now my concern here is that Is it possible to fetch all the thread of a particular(by using some filters) email using Mail.dll component?

And also provide some sample code so that I can proceeds further with Mail.dll.

Thanks
Ashok

by (250 points)
edited by

1 Answer

0 votes

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);
    }
}
by (297k points)
...