See http://googledevelopers.blogspot.se/2012/09/adding-oauth-20-support-for-imapsmtp.html and https://developers.google.com/accounts/docs/OAuth2