0

我正在尝试通过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"
4

1 回答 1

0

超时是因为您连接的端口是服务器之间的电子邮件路由,而不是邮件提交代理端口。使用端口 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')
于 2013-06-22T17:36:47.333 回答