Introduction:
The other day I was asked to install a completely new email server and transfer all the email accounts from the old mail server to the new one. I noticed that since the new mail server was using a different mail INBOX format I had to do some research and found this really good tool to do exactly what I needed called: imapsync

Installing the tool:
This tool programmed in Perl and is not free. It can be bought at http://imapsync.lamiral.info/.
Note: It does a great job and it’s really worth its price when you think of the time and hassle saved by using it.
You will probably need to install some extra needed Perl Modules as follows:

sudo apt-get install libfile-copy-recursive-perl
sudo apt-get install libio-socket-ssl-perl
sudo apt-get install libio-tee-perl
sudo apt-get install libunicode-string-perl
cpan Mail::IMAPClient
cpan Term::ReadKey
cpan Digest::MD5
cpan Term::ReadKey
cpan IO::Socket::SSL
cpan File::Spec
cpan Digest::HMAC_MD5
cpan Authen::NTLM
cpan Readonly
cpan Sys::MemInfo
cpan Regexp::Common
cpan App::cpanminus
cpanm IO::Tee
cpanm Mail::IMAPClient
cpanm Unicode::String
cpanm Sys::MemInfo
cpanm File::Tail

If you are on Ubuntu you can run the following command to install those needed Perl Modules:

apt update && apt install libio-tee-perl libmail-imapclient-perl libterm-readkey-perl libunicode-string-perl libreadonly-xs-perl libsys-meminfo-perl libregexp-common-perl libfile-tail-perl

Using the tool:
Example 1: Copying all the mails in folder INBOX from jim account on localhost to another server with the same credentials:
– First we do a dry-run to see what will be transferred when I run it normally:

imapsync --dry \
      --host1 localhost          --user1 jim --password1 'secret1' --folder INBOX --tls2 \
      --host2 mail.myserver2.com --user2 jim --password2 'secret1' --nofoldersizes --nofoldersizesatend

Example 2: Copying all the mails and folders(no dry-run) from account martin@myserver1.com on localhost to a new account on another server with different credentials:

imapsync \
   --host1 localhost --user1 martin@myserver1.com --password1 secret1 \
   --host2 mail.myserver2.com --user2 martin@myserver2.com --password2 secret2

Example 3: Migrating full account from GMAIL to a private IMAP account on eg. Linux:

imapsync --no-modulesversion --nofoldersizes --ssl1 --ssl2 \
  --gmail1 --user1 MyUser1@gmail.com --password1 '********' 
  --host2 myhost2.srv --user2 MyUser2@myswerver.com --password2 'xxxxxxxxxx'