OS X 10.6.8, Apple Mail
There may be another way to do this, but this method works for IMAP, and others I tried do not.
You decide to migrate your domain name to another host and so your mailbox (eg. firstname.lastname@example.org) is going to be deleted.
You will need to migrate the domain and setup your mail box on the new host. Once you’ve done that, here’s how to setup the new account on your Mac to retain and access your old mail.
Quit Apple Mail. Disconnect from the internet (unplug the cord, turn off Airport… whatever your setup uses).
Copy the following folders in your user’s Library to the desktop, just in case.
Launch Apple Mail. Create a new Mailbox (Apple Mail > Mailbox > New Mailbox > On This Mac), call it something like `Archive – Inbox`. Once it’s created, copy/move all your messages from your inbox to the `Archive – Inbox` folder.
Repeat this process for each folder you have (Sent, Drafts, etc.) including any custom folders you have set up.
Connect The New Mailbox
Once your archive mailboxes have been set up and you have migrated all your mail, reconnect to the internet. Restart Apple Mail. IMAP will do it’s magic and synchronise your mail, removing anything that is not on the remote server.
It’s worth noting that there are a few alternatives to this, such as migrating the mail across at the web host level. But if you need to migrate locally, this method works a treat.