Python:使用smtplib获取Gmail服务器永远不会结束

时间:2016-09-15 16:36:59

标签: python smtplib

我只是尝试过:

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

在我的Python解释器中,但第二个语句永远不会结束。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分: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