所以我尝试使用简单的脚本使用POP访问gmail,几乎与此处Checking email with Python完全相同,除非从密码文件中获取用户名和密码。
import poplib
from email import parser
from secrets import *
pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user(username)
pop_conn.pass_(password)
#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message into an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
print message['subject']
pop_conn.quit()
但是,这会产生以下错误:
Traceback (most recent call last):
File "checkmail.py", line 7, in <module>
pop_conn.pass_(password)
File "/usr/lib/python2.7/poplib.py", line 197, in pass_
return self._shortcmd('PASS %s' % pswd)
File "/usr/lib/python2.7/poplib.py", line 160, in _shortcmd
return self._getresp()
File "/usr/lib/python2.7/poplib.py", line 136, in _getresp
raise error_proto(resp)
poplib.error_proto: -ERR [AUTH] Web login required: https://support.google.com/mail/answer/78754
阅读该链接以及Google发送的电子邮件,解释说他们基于安全原因阻止了此脚本的访问。可以禁用此安全性,但我不愿意。所以我的问题是,如何让这个脚本通过安全检查。
欢呼任何帮助。