Home > database >  How to see top 20 e-mails with C#
How to see top 20 e-mails with C#

Time:01-05

I am trying to do a mail app with Windows Forms C#. I only want to see top 20 rows of my inbox.

Edit: Code is working fine but only listing me 20 random emails in my inbox

I've tried this:

using (var client = new Pop3Client())
        {
            client.Connect("pop.gmail.com", 995, true);
            client.Authenticate("mail", "passwrd");

            for (int i = client.Count - 20; i < client.Count; i  )
            {
                var message = client.GetMessage(i);
                Console.WriteLine("Subject: {0}", message.Subject);
                txtBoxMails.AppendText("Subject: "   message.Subject   "\n");
            }

            client.Disconnect(true);
        }

CodePudding user response:

You can use GetMessages. It takes 2 parameters (int startIndex, int count). The index of the first message to get and how many messages.

An example without testing it.

var messages = client.GetMessages(0,20);

foreach (var item in messages)
{
    Console.WriteLine(item.Subject);
}

CodePudding user response:

you should use GetMessageCount instead of Count

var messageCount = client.GetMessageCount();
var lastMessageIndex = messageCount-20;
for (int i = messageCount; i > lastMessageIndex; i--)
{
    //Do
}
  • Related