Imapsynchronizer is a project mainly written in Java, it's free.
IMAP sync tool
IMAP sync tool, for synchronizing IMAP mail between two servers. Has special logic for dealing with Exchange (ugh) servers, if needed.
Build with "mvn package"
Run with "java -jar target/imapsynchronizer-VERSION.jar", options are explained when none are given.