Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download only new emails from imap?

I have an application that is used to archive emails using imap. Also in this application are many imap accounts that need to be archived.

In this moment from time to time the application connects to imap accounts and download only new emails. My issue is that every time when it connects to an imap account it verifies all emails from all folders and downloads only emails that aren't downloaded yet (I store Message-ID for all emails and download only emails that have an Message-ID that is not stored). So I want to know if there is an alternative for this, because it takes some time to verify all emails (for 10-20K it takes 2-5 minutes).

I use JavaMail API to connect to imap accounts.

like image 745
telebog Avatar asked Jan 18 '11 14:01

telebog


People also ask

How do I stop IMAP from downloading old messages?

A similar issue can be solved by going to Account Settings, your default account, More Settings, and Advanced. Find the “leave a copy of messages on the server” and remove the check. Note that this can make old messages inaccessible, so make sure your message archives are backed up.

How do I stop Outlook from downloading old emails?

Choose received in a specific date span Click the underlined words and tick Before: [date]. Choose yesterday's date if you haven't downloaded mail yet today. Click Ok then Next. On the actions page, select either move to the specified folder then select the folder or permanently delete it.

Can I download IMAP emails?

You can save your backup IMAP email in any form like PST, EML, MSG, MHT, HTML, DOC, DOCX, and PDF.

Does IMAP download all emails from server?

Users can take backup of unlimited emails and can download selective emails from the IMAP account. Moreover, all the files and folders will appear on the screen, so users can directly select data. The software enables you to download all emails from IMAP server into any desired file format.


1 Answers

The javadoc helps:

IMAPFolder provides the methods:

getMessagesByUID(long start, long end) and

getUID(Message message)

With getUID() you can get the UID of the last message you already have downloaded. With getMessagesByUID you can define this last message you have downloaded as start-range and look with the method getUIDNext() to find the last message which would be the end of the range.

like image 148
Erik Avatar answered Nov 01 '22 10:11

Erik