IMAP Problem retrieving only last 100 messages

0 votes
asked Oct 6, 2016 by pilouk (520 points)
edited Oct 7, 2016 by pilouk

I have problem retrieving last 100 message

IMAP.SelectFolder("Inbox")

Dim folderMessage As ImapMessageCollection
mgsSet = New ImapMessageSet
mgsSet.AddRange(1, 100)
folderMessage = IMAP.GetMessageList(mgsSet)

This return : The specified message set is invalid (NO).

Applies to: Rebex Secure Mail

2 Answers

+1 vote
answered Oct 7, 2016 by Tomas Knopp (58,560 points)
selected Oct 7, 2016 by pilouk
 
Best answer

Most probably your folder does not contain 100 messages and that's why you get the error. Try this code:

Dim imap As New Imap()
imap.Connect("server")
imap.Login(...)
imap.SelectFolder("INBOX")

' maximum number of newest messages to retrieve
Dim limit As Integer = 100

' calculate the range (there can be less messages in the inbox than the limit specified)
Dim last As Integer = imap.CurrentFolder.TotalMessageCount
Dim first As Integer = Math.Max(1, last - limit + 1)

' construct an ImapMessageSet
Dim messageSet As New ImapMessageSet()
messageSet.AddRange(first, last)

' retrieve the messages

Dim messages = imap.GetMessageList(messageSet)

Console.WriteLine("count = " + messages.Count.ToString())

' iterate through the messages (from newer to older) and write subject:
For index = messages.Count - 1 To 0 Step -1
        Console.WriteLine(messages(index).Subject)
Next
commented Oct 7, 2016 by pilouk (520 points)
Thanks man !
0 votes
answered Oct 7, 2016 by pilouk (520 points)
edited Oct 7, 2016 by pilouk

The range must not exceed the folder message count.

...