Of all the services hosted by OS X Server that are apparently going away, none fills me with greater joy on the occasion of its demise than the Mail service. I mean, it was fine if you liked that sort of thing. If you liked your email basic IMAP and POP and you liked it flexible but slow, and if you enjoyed the delightful frisson of high-wire horror whenever hardware failure knocked a server out and rendered an enormous client dead in the water then this was your cup of tea.
Seth and I could probably sit down and talk for upwards of an hour solid about the nightmares this misbegotten monstrosity has foisted on us over the years. The time that we went in after hours on a Friday night to set up a server, and the brand spanking new mail server RAID died two hours into it's short life and turned the plan of being home by 9pm into being home at 4am. The time that a weird postfix error ate every user account the day before the company was due to present their multi-million dollar proposal to the Department of Agriculture. Its propensity of greylisting emails on the basis that timely communication was an aberration, or at least an amusing foible. I can feel my arteries scarring just writing these words.
Thankfully, hosted Exchange and Gsuite have quietly gone from strength to strength and recent release of iOS and macOS client have essentially become transparent platforms for those services, and most organizations have moved away from hosting mail internally to the point that the ones that do are anachronisms. Still, there are a few out there. Lurking.
But no more. On the off-chance that you are one of those holdouts (and if you are you should feel nothing but deep, abiding shame) or ever have to deal with one then you'll need a way to migrate those accounts to something new pretty sharpish. Enter imapsync - which you can download and install from the package manager of your choice. I like Homebrew, so if you don't have that on your Mac already then you can download it from http://brew.sh or just paste the following into the Terminal:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Once that's installed, type the following into the Terminal:
brew install imapsync
...then sit back and wait a minute or two for it to install.
Once that's installed you'll need to figure out the mail domains and the accounts on the server. Luckily - if you've installed the Server.app on that Mac - you'll be able to use the serveradmin tool to figure that out for you thus:
sudo serveradmin settings mail:postfix:domains:_array_index:
Digging out the accounts is pretty straightforward too - just take a look at the virtual_users file here:
The next part can get a little tricky - particularly if you're moving domains - and requires establishing a good time to cut over mail to the new host. Change the MX records for the domain (let's call it madeupname.com) over to point mail to the new server then use imapsync to push the old mail to the new account thus:
imapsync --host1 oldmail.madeupname.com --user1 dave_ball --password1 myoldemailpassword --host2 newhostedmail.madeupname.com --user2 dave_ball --password2 mynewemail password
This can take a long time. If you have a lot of accounts then it's worth exploring importing mail directly (both Gsuite and Exchange have assorted ways to accomplish that), but if you're just dealing with a small number of accounts then imapsync is pretty simple and easy.