3

I wrote code to read emails with Mailkit, in the Mailkit there is a way to read first email, but i couldn't find a way to read subject of last recent email

using (var client = new ImapClient())
                {
                    client.Connect(EXT_IMAP_SERVER, EXT_IMAP_PORT, true);

                    client.Authenticate(EXT_USERNAME, EXT_PASSWORD);

                    var inbox = client.Inbox;
                    inbox.Open(FolderAccess.ReadWrite);
                    var LAST_EMAIL = inbox.FirstUnread;// ****** here is my issue ******
                    var LAST_MSG = client.Inbox.GetMessage(LAST_EMAIL);
                    inbox.AddFlags(LAST_EMAIL, MessageFlags.Seen, true);
                    Console.WriteLine(LAST_MSG.Subject);

                    client.Disconnect(true);
           
                }
Ali Kazemi
  • 505
  • 7
  • 19

1 Answers1

3

in the Mailkit there is a way to read first email

The first email in the folder always has an index of 0. The FirstUnread property is the index of the oldest message in the folder that has not yet been read.

In other words, if your folder has 100 messages but you haven't checked mail in a few days, you might have a few unread messages. Maybe the last 3 messages are unread, so the indexes of those unread messages would be 97, 98, and 99. The FirstUnread index would be 97 in this example.

but i couldn't find a way to read subject of last recent email

The index of the last message is always folder.Count - 1. Just like the index of the last element in an array is always array.Count - 1.

Or to make it even clearer, if you have a folder with 100 messages in it, the index of the last message in the folder will be 99.

var message = folder.GetMessage (folder.Count - 1);
Console.WriteLine (message.Subject);
jstedfast
  • 35,744
  • 5
  • 97
  • 110