Errno 10060 python通过gmail发送电子邮件

时间:2013-06-22 16:55:03

标签: python email gmail

我正在尝试使用gmail通过python 2.7帐户发送电子邮件。我的代码如下,任何帮助表示赞赏!我一直在说:

  

Errno 10060-连接尝试失败,因为连接方没有   一段时间后适当回应......

import smtplib

FROMADDR = "myemail@gmail.com"
LOGIN    = FROMADDR
PASSWORD = "mypassword"
TOADDRS  = "toEmail@gmail.com"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 25, timeout=120)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"

1 个答案:

答案 0 :(得分:0)

超时是因为您在端口for email routing between servers而不是mail submission agent端口上进行连接。使用端口587时,它对我来说很好:

>>> import smtplib
>>> server = smtplib.SMTP('smtp.gmail.com',587)
>>> server.ehlo()
(250, 'mx.google.com at your service, [99.178.174.213]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES')
>>> server.starttls()
(220, '2.0.0 Ready to start TLS')
>>> server.ehlo
<bound method SMTP.ehlo of <smtplib.SMTP instance at 0x1e518c0>>
>>> server.login('xxxxxxxxxxxx','xxxxxxxx')
(235, '2.7.0 Accepted')
>>> msg = "test message"
>>> server.sendmail('xxxxxxxxxxxx@gmail.com','xxxxxxx@yahoo.com',msg)
{}
>>> server.quit()
(221, '2.0.0 closing connection xxxxxxxxxxxxx.x - gsmtp')