After two years of using Gmail, I have exactly 5,531 emails. These are all sitting on some server who knows where, and getting them all off of there and onto my own server was an interesting task.
It turned out to be pretty easy thanks to Thunderbird. If you enable POP in Gmail, you can use Thunderbird to access Gmail that way. So I set that up, and set Gmail to serve up all messages including read / archived ones. Then I let Thunderbird fetch them all. This took a while (maybe an hour?).
The thing that makes this work is that Thunderbird stores mail locally as plain old mbox format files. So if you browse to ~/.thunderbird/blargaljdf.default/Mail/pop.gmail.com, you can see files with names matching the "mailboxes" in Thunderbird. There are multiple files per "mailbox". The one without an extension is the mbox file.
All I needed to do was upload these mbox files to my IMAP server and subscribe to them, and that's it. I actually used Thunderbird to split up all those Gmails into multiple mbox files first and then uploaded those. The Thunderbird GUI makes this all fairly easy. I have no use for Thunderbird normally, but it's good for this task.
It strikes me that this would also be a good way to make a backup of Gmail, if someone still plans to use it. Or any other POP email service, I suppose.
Interestingly, doing this caused IlohaMail to meltdown. It would say it knew I had 500 emails in folder X, but it would only let me see 120 of them. So now I'm using Horde again. Someday, I'll find a webmail program that works. Someday.