I found an interesting program called getmail for linux (which you can install through ‘apt-get install getmail4’ on Ubuntu), its used for fetching mail from a mail server and delivering it to a folder or MTA.
The following is the configuration file I used to download email from my gmail account to keep a back up (you need to go to setting in GMail if you want to retrieve all your messages).
I found Getmail was only pulling the first 349 emails for me, by adding an options section to the config file with ‘delete = true’ i managed to download all my email. It downloaded messages in batches so i had to run getmail a number of times to get all my email. Interestingly ‘delete = true’ doesn’t seem to actually delete the message for the GMail webbased interface, but it does mean you can’t retrieve it again via POP.
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = email_address
password = password
type = Maildir
path = ~/Mail/your-maildir-folder
delete = true