0 votes
by (120 points)
edited

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
by (520 points)
edited

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());
}
...