If you follow the link http://www.google.com/support/accounts/bin/answer.py?answer=185833 it will show you how to generate an application specific password. As for attachments, that is beyond the scope of this snippet. I would have to add MIME support, which I am not going to do.