You can do everything you need with JavaMail. Here is the API
Here is an example of getting all unread messages from an inbox and marking them as read. Take a look at the folder class (specifically the copyMessages()
method) to move messages to a new folder.
import java.util.Properties;
import javax.mail.*;
import javax.mail.search.FlagTerm;
public class Driver {
public static void main(String[] args){
// Create properties (disable security checks on server)
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
try{
// Get the store
Store store = session.getStore("pop3");
store.connect("servername", "username", "password");
//connection configuration
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
//get all unread messages in the inbox
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), true);
Message[] messages = folder.search(ft);
for (int i = messages.length -1; i>=0; i--) {
messages[i].setFlag(Flags.Flag.SEEN, true);
}
// Close connection
folder.close(false);
store.close();
}
catch(Exception e){
e.printStackTrace();
}
}