Sorting POP3 message list

0 votes
asked Jul 30, 2010 by Oguz (120 points)
edited Dec 7, 2010

Hi,

I want to sort the my messages by date. SO the newest email must show at the top. How can I do this? I am using the following code.

Pop3 client = new Pop3(); client.Connect("pop.gmail.com",995,null, Pop3Security.Implicit);

        client.Login("xxx", "xxx");

        Pop3MessageCollection messages = 
          client.GetMessageList(Pop3ListFields.FullHeaders);

        // display info about each message   
        Console.WriteLine("UID | From | To | Subject");
        foreach (Pop3MessageInfo message in messages)
        {
            Console.WriteLine(
               message.UniqueId.ToString() + "|" + 
               message.From.ToString() + "|" + 
               message.To.ToString() + "|" + 
               message.Subject.ToString());
        }

        client.Disconnect();
Applies to: Rebex Secure Mail

1 Answer

+1 vote
answered Jul 30, 2010 by Ivan Prochazka (520 points)
edited Dec 7, 2010

Hi,

if you are using .NET 3.5 or later you can do it by using LINQ extension method OrderBy() or OrderByDescending(), so your code will now like this:

Pop3MessageCollection messages = 
  client.GetMessageList(Pop3ListFields.FullHeaders);    

IEnumerable<Pop3MessageInfo> orderedAsc = 
   messages.OrderBy(m => m.Date.UniversalTime);

// display info about each message   
Console.WriteLine("UID | From | To | Subject");

foreach (Pop3MessageInfo message in orderedAsc)
{
     Console.WriteLine(
        message.UniqueId.ToString() + "|" + 
        message.From.ToString() + "|" +      
        message.To.ToString() + "|" + 
        message.Subject.ToString());
}
...