简体   繁体   中英

How to see top 20 e-mails with C#

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

Edit: The 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);
}

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

you should use GetMessageCount instead of Count

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM