Python:使用smtplib获取Gmail服务器永远无法结束

3

我只是尝试了一下:

>>> import smtplib
>>> server = smtplib.SMTP('smtp.gmail.com:587')

在我的 Python 解释器中,但第二个语句永远不会结束。
有人能帮忙吗?

你修好了吗? - cangrejo
1个回答

0

你可能会发现,在成功登录之前,需要先输入用户名和密码。
可以尝试以下操作:

import smtplib
ServerConnect = False
try:
    smtp_server = smtplib.SMTP('smtp.gmail.com','587')
    smtp_server.login('your_login', 'password')
    ServerConnect = True
except SMTPHeloError as e:
    print "Server did not reply"
except SMTPAuthenticationError as e:
    print "Incorrect username/password combination"
except SMTPException as e:
    print "Authentication failed"

如果你遇到“连接意外关闭”的问题,请尝试将服务器行更改为:
smtp_server = smtplib.SMTP_SSL('smtp.gmail.com','465')

注意:Google 可能会阻止一些不使用现代安全标准的应用程序或设备的登录尝试。由于这些应用程序和设备更容易被入侵,因此阻止它们有助于保护您的帐户安全。详情请见:https://support.google.com/accounts/answer/6010255?hl=en

Gmail 设置:

SMTP Server (Outgoing Messages)     smtp.gmail.com  SSL     465     
    smtp.gmail.com  StartTLS    587
IMAP Server (Incoming Messages)     imap.gmail.com  SSL     993    
    Please make sure, that IMAP access is enabled in the account settings.
Login to your account and enable IMAP.
You also need to enable "less secure apps" (third party apps) in the Gmail settings:
https://support.google.com/accounts/answer/6010255?hl=en
See also: How to enable IMAP/POP3/SMTP for Gmail account

如果其他方法都失败了,可以尝试从命令行执行ping gmail.com

1
实际上,smtp_server.login('your_login', 'password') 永远不会被执行。解释器停留在前一行。 - MarAja
然后将其放入 try...except.. 语句中并打印出错误。 - Rolf of Saxony
1
我没有收到任何错误信息,这就是我的问题。解释器一直卡住,什么也没有打印出来。SMTP_SSL 也无法工作。我得到了一个“SSL: UNKNOWN_PROTOCOL”的错误。 - MarAja
看一下我的新编辑,在Python解释器中输入import smtplib,然后输入help(smtplib.SMTP_SSL)。如果失败了,那么你的smtplib库可能有问题。如果成功了,请继续输入上面的代码,直到得到结果。 - Rolf of Saxony

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接