Moving email servers has always been a royal pain for me. I’ve tried many of them over the years, and migrated from nearly as many of them. However the last couple of times I put myself through the pain I came across imapsync – an excellent script that will do an IMAP to IMAP migration across the wire.
Imapsync uses standard IMAP commands throughout, so is ideal for migrating from one brand of email server to another. (Of course there are still buggy IMAP servers out there, but well behaved ones will work fine.) It will transfer emails, folders, maintain message flags and folder subscriptions.
For best performance use the script on your new server (presumably it is much more powerful that the one you are migrating from) and make sure any obnoxiously large emails, junk, trash and other unwanted mail has been deleted (and expunged) first (see
Debug your IMAP server with Telnet for help with this.
Imapsync is written in Perl and is happy on almost any Operating System (yes even Windows!)