Ensure IMAP flushes data to disk before quitting. Closes #5095.