I had some old personal email accounts that I wanted to copy over
to GMail so that they would be more easily searchable. There is a
imapsync that makes this easy.
You can get this script from either Debian ports and macports.
The command line that I used was something like (fill in the variables indicated by $). It was for moving from the old account to gmail.
imapsync --host1 $UNI_HOST_NAME --user1 $UNI_USER_NAME --port1 993 --authmech1 PLAIN --noauthmd5 --syncinternaldates --user2 $GOOGLE_USER_NAME --host2 imap.gmail.com --port2 993 --ssl1 --ssl2 --useheader Message-Id --folder $NAME_OF_IMAP_FOLDER --skipsize --password1 $UNI_PASSWD --password2 $GOOG_PASSWD --regextrans2 's/INBOX/All Mail/'
I stuck this within a shell script that iterator over all of my folders on the from side. GMail automatically maps labels to folders in its IMAP implementation. There are a few idiosyncracies in this mapping that I don't remember.