我有下面的代码,我正在jupyter笔记本中运行。 我试图登录到yahoo电子邮件帐户,并从一个文件夹中下载所有的电子邮件。 当我尝试运行代码时,我得到了下面的错误。 当我只是去yahoo.com用相同的电子邮件地址和密码登录时,它可以正常工作。 有人知道问题出在哪里吗? 下面的邮件是一个假的例子)。
代码来自github repo。https:/gist.github.comrobulouski7442321。
代码:
import imaplib, email, getpass
import sys
from email.utils import getaddresses
IMAP_SERVER = 'imap.mail.yahoo.com'
EMAIL_ACCOUNT = '[email protected]'
EMAIL_FOLDER = 'TheFolder'
OUTPUT_DIRECTORY = '/Users/name/Desktop/Stuff/NewFolder'
PASSWORD = getpass.getpass()
def process_mailbox(M):
"""
Dump all emails in the folder to files in output directory.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
print("Writing message ", num)
f = open('%s/%s.eml' %(OUTPUT_DIRECTORY, num), 'wb')
f.write(data[0][1])
f.close()
def main():
M = imaplib.IMAP4_SSL(IMAP_SERVER)
M.login(EMAIL_ACCOUNT, PASSWORD)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print("Processing mailbox: ", EMAIL_FOLDER)
process_mailbox(M)
M.close()
else:
print("ERROR: Unable to open mailbox ", rv)
M.logout()
if __name__ == "__main__":
main()
错误:
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-8-972361fa1b80> in <module>()
1 if __name__ == "__main__":
----> 2 main()
<ipython-input-5-28d5fe9c1500> in main()
21 def main():
22 M = imaplib.IMAP4_SSL(IMAP_SERVER)
---> 23 M.login(EMAIL_ACCOUNT, PASSWORD)
24 rv, data = M.select(EMAIL_FOLDER)
25 if rv == 'OK':
~/anaconda/envs/py36/lib/python3.6/imaplib.py in login(self, user, password)
591 typ, dat = self._simple_command('LOGIN', user, self._quote(password))
592 if typ != 'OK':
--> 593 raise self.error(dat[-1])
594 self.state = 'AUTH'
595 return typ, dat
error: b'[AUTHENTICATIONFAILED] LOGIN Invalid credentials'
更新。
我进入我的雅虎账户安全设置,并更新 "允许使用不太安全的登录的应用程序",允许。 现在,当我在jupyter笔记本中运行代码时,我得到了下面的错误信息。
错误。
---------------------------------------------------------------------------
abort Traceback (most recent call last)
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _command_complete(self, name, tag)
1013 try:
-> 1014 typ, data = self._get_tagged_response(tag)
1015 except self.abort as val:
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_tagged_response(self, tag)
1133 try:
-> 1134 self._get_response()
1135 except self.abort as val:
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_response(self)
1041
-> 1042 resp = self._get_line()
1043
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _get_line(self)
1145 if not line:
-> 1146 raise self.abort('socket error: EOF')
1147
abort: socket error: EOF
During handling of the above exception, another exception occurred:
abort Traceback (most recent call last)
<ipython-input-14-3de4ef460c62> in <module>()
39
40 if __name__ == "__main__":
---> 41 main()
42
<ipython-input-14-3de4ef460c62> in main()
29 M = imaplib.IMAP4_SSL(IMAP_SERVER)
30 M.login(EMAIL_ACCOUNT, PASSWORD)
---> 31 rv, data = M.select(EMAIL_FOLDER)
32 if rv == 'OK':
33 print("Processing mailbox: ", EMAIL_FOLDER)
~/anaconda/envs/py36/lib/python3.6/imaplib.py in select(self, mailbox, readonly)
738 else:
739 name = 'SELECT'
--> 740 typ, dat = self._simple_command(name, mailbox)
741 if typ != 'OK':
742 self.state = 'AUTH' # Might have been 'SELECTED'
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _simple_command(self, name, *args)
1189 def _simple_command(self, name, *args):
1190
-> 1191 return self._command_complete(name, self._command(name, *args))
1192
1193
~/anaconda/envs/py36/lib/python3.6/imaplib.py in _command_complete(self, name, tag)
1014 typ, data = self._get_tagged_response(tag)
1015 except self.abort as val:
-> 1016 raise self.abort('command: %s => %s' % (name, val))
1017 except self.error as val:
1018 raise self.error('command: %s => %s' % (name, val))
abort: command: SELECT => socket error: EOF
你有没有用雅虎邮件来解决这个问题?我有类似这样的代码,可以用gmail,但不能让它与雅虎工作。