通过Python错误发送电子邮件smtp

时间:2010-05-27 03:29:30

标签: python email smtp

我无法弄清楚为什么这不起作用。我正在尝试使用我上网的这段代码从我的学校电子邮件地址发送一封电子邮件。相同的代码适用于从我的GMail地址发送。有谁知道这个错误意味着什么?等待大约一分半钟后发生错误。

import smtplib

FROMADDR = "FROM_EMAIL"
LOGIN    = "USERNAME"
PASSWORD = "PASSWORD"
TOADDRS  = ["TO_EMAIL"]
SUBJECT  = "Test"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
    % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text\r\n"

server = smtplib.SMTP('OUTGOING_SMTP', 465)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()

这是我得到的错误:


    Traceback (most recent call last): 
      File "emailer.py", line 13, in 
    server = smtplib.SMTP('OUTGOING_SMTP', 465)
    File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in init
    (code, msg) = self.connect(host, port)
   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 514, in create_connection
    raise error, msg
socket.error: [Errno 60] Operation timed out

1 个答案:

答案 0 :(得分:2)

您学校的SMTP服务器可能不允许外部访问端口587. Gmail会执行,并且需要身份验证才能确保您就是您所说的人(并且垃圾邮件发送者无法发送电子邮件似乎是来自你,除非他们知道你的密码)。您的学校可能已选择设置其邮件服务器,以便只有来自 学校内的连接才能以这种方式发送邮件。