将imaplib与yahoo结合使用时出现凭证错误

时间:2018-09-23 21:16:38

标签: python-3.x imaplib yahoo-mail

我有下面的代码在jupyter笔记本中运行。我正在尝试登录yahoo电子邮件帐户并从文件夹下载所有电子邮件。当我尝试运行代码时出现以下错误。当我只是去yahoo.com并使用相同的电子邮件地址和密码登录时,它可以正常工作。有人知道这个问题可能是什么吗? (以下电子邮件是伪造的示例。)

代码来自github仓库: https://gist.github.com/robulouski/7442321

代码:

import imaplib, email, getpass
import sys
from email.utils import getaddresses


IMAP_SERVER = 'imap.mail.yahoo.com'
EMAIL_ACCOUNT = 'MadeUp@yahoo.com'
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'

更新:

我进入了yahoo帐户安全设置,并更新了“允许使用安全性较低登录名的应用程序”进行允许。现在,当我在jupyter Notebook中运行代码时,出现以下错误消息。

错误:

---------------------------------------------------------------------------
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

1 个答案:

答案 0 :(得分:0)

您是否曾经使用过Yahoo Mail?我有与此类似的代码可与gmail一起使用,但无法使其与Yahoo一起使用。