Archived Forum Post

Index of archived forum posts

Question:

Imap fetch last 50 messages - how?

Oct 23 '13 at 17:23

Hello all using VB6, ActiveX, how to select last 50 (newest) message headers, if I have 4000 messages? THanks.


Answer

If the Chilkat IMAP reference documentation is perused, it will be discovered that there are two ways of fetching email from an IMAP server -- by UID or by sequence number.

The UID itself does not imply any ordering, but it uniquely identifies an email. Sets of UID's can be obtained via Search on various criteria -- but this provides no way of getting the most recent N emails. (It might provide a way of getting all emails that are less than one week old, but not a pre-determined result set size.)

A sequence number, however, implies an ordering to the emails in a mailbox. The first email to be inserted in the mailbox is at sequence number 1, the 2nd at 2, etc. If the 1st email is deleted, then what was previously the email at sequence number 2 now becomes the email at sequence number 1.

After selecting a mailbox, the Chilkat.Imap.NumMessages property contains the number of messages in the mailbox. (This is stated in the reference documentation.) If there are 100 emails in the mailbox, and you wish to download the 50 newest, and the oldest email is at sequence number 1, then you would download emails from sequence number 51 to 100. The reference documentation can be quickly reviewed to see what methods are available for downloading by sequence number. You'll find several options -- you may download them individually or altogether in a set by specifying the starting sequence number and a count.


Answer

Yes. You should pass a starting sequence number and a count to FetchSequenceHeaders. If you want to fetch the last 50 emails, assuming the Inbox has more than 50 emails, you would do this:

cnt = imap.NumMessages
bundle = imap.FetchSequence cnt-49, 50
Remember: The 2nd argument is the count of the number of emails you wish to fetch. Also remember that the 1st email is at sequence number 1. For example, if there are exactly 50 emails in Inbox, then you can see that subtracting 49 is correct because 50 - 49 = 1. The starting sequence number would be 1 and you'd be fetching 50 emails.


Answer

Thank you for your answer.

when I try this:

cnt = imap.NumMessages

bundle = imap.FetchSequenceHeaders cnt-50, cnt

bundle is empty

What is wrong here?

Thanks


Answer

OK sorry, I'm new user :( here is LastErrorText, after I call:

cnt = imap.NumMessages
bundle = imap.FetchSequenceHeaders cnt-50, cnt

ChilkatLog: FetchSequenceHeaders: DllDate: Aug 15 2013 ChilkatVersion: 9.4.1.42 UnlockPrefix: xxxxxxx Username: xxxxxx Architecture: Little Endian; 32-bit Language: ActiveX VerboseLogging: 0 Failed to find OK line in response. Failed to fetch sequence range of summaries --FetchSequenceHeaders --ChilkatLog

cnt have value 4100

Thanks!


Answer

This code works OK:

cnt = imap.NumMessages
bundle = imap.FetchSequence cnt-50, cnt
but it is too slow ... 25 seconds to display 50 messages.

same code but using FetchSequenceHeaders, produces this error (used verboseLogging):

ChilkatLog:
  FetchSequenceHeaders:
    DllDate: Aug 15 2013
    ChilkatVersion: 9.4.1.42
    UnlockPrefix: xxx
    Username: xxx
    Architecture: Little Endian; 32-bit
    Language: ActiveX
    VerboseLogging: 1
    ImapCmdSent: aaad FETCH 4388:8825 (UID FLAGS RFC822.SIZE BODYSTRUCTURE BODY.PEEK[HEADER])
    getCompleteResponse2:
      rcvUntilMatchStringQP: 

  dbReceived0: 
  startIdx: 0
  dbReceived: aaad BAD Error in IMAP command FETCH: Invalid messageset

  Found match string.
  ImapCmdResp: aaad BAD Error in IMAP command FETCH: Invalid messageset
--getCompleteResponse2
msg: aaad BAD Error in IMAP command FETCH: Invalid messageset

Failed to find OK line in response.
Failed to fetch sequence range of summaries

--FetchSequenceHeaders --ChilkatLog

Any idea why?

Thanks.


Answer

Of course, it working perfect now.

Thanks a lot.