Say your are at hosting company 'A' and you want to move to a new hosting company (for arguments sake, how about us? The only problem is you have lots of email stored on your old host's server. In IMAP folders. Possibly for lots of users. And maybe for a number of domains.
So: how do you migrate your email to your new server? You have a few options.
Contrary to popular opinion low tech solutions are often actually good options. Do the simplest thing possible (but no simpler).
In this case the solution is to add your new server imap details to your email client. Then drag and drop or copy email folders from one server to the other.
Sometimes dragging and dropping is not feasible. For example, you may be doing this for lots of email users and it is not practical for them to all do the dragging and dropping.
In this case you can copy over the mail files. Use something like rsync (if you have ssh access to both servers) or FTP (if you lack ssh capability on the original host).
If each email 'folder' is stored in a single file then you have your email in the 'mbox' format (see https://en.wikipedia.org/wiki/Mbox). This is the most common mail format. It also happens to be the default format for most older imap servers (e.g. uw-imap on WBEL3 and dovecot on RHEL4/Debian).
If you are copying over mbox files just pop them in each email user's home directory. Most commonly they are put in a subdirectory like 'Mail'. e.g. your Sent Items folder would be at "/home/$username/Mail/Sent Items"
You will also need the user's 'Inbox'. That file is typically stored at /var/spool/mail/$username.
If your original IMAP emails are in thousands of files in directories like tmp, cur or new then you are not using the mbox format. You are using the newer maildir format (see https://en.wikipedia.org/wiki/Maildir).
Dovecot supports this email storage format. So you should be able to copy over the maildir directories and configure dovecot to use maildir instead of mbox.
IMAP Sync is a command-line (Perl) tool to sync IMAP mailboxes. You give it a source and target hostname, username, passwords and folders to copy then it syncs those mailboxes.