sendmail成功,但python smtplib连接失败

发布于 2024-12-03 12:53:24 字数 2095 浏览 1 评论 0原文

这有效:

printf 'hi' | sendmail -f [email protected] [email protected]

但这失败:

def send_mail(send_from, send_to, subject, text, files=[ ], server="localhost"):
    assert type(send_to)==list
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    msg.attach( MIMEText(text) )
    for f in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(f, "r").read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        msg.attach(part)
    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

输出:

Traceback (most recent call last):
  File "send_mail.py", line 50, in <module>
    send_mail(send_from, send_to, subject, text, files )
  File "send_mail.py", line 35, in send_mail
    smtp = smtplib.SMTP(server)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 61] Connection refused

我如何让我的 send_mail 方法正常工作?

this works:

printf 'hi' | sendmail -f [email protected] [email protected]

but this fails:

def send_mail(send_from, send_to, subject, text, files=[ ], server="localhost"):
    assert type(send_to)==list
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    msg.attach( MIMEText(text) )
    for f in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(f, "r").read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        msg.attach(part)
    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()

Output:

Traceback (most recent call last):
  File "send_mail.py", line 50, in <module>
    send_mail(send_from, send_to, subject, text, files )
  File "send_mail.py", line 35, in send_mail
    smtp = smtplib.SMTP(server)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 61] Connection refused

How do i get my send_mail method working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-12-10 12:53:24

重新发布提出该问题的用户的答案:

确实,没有任何内容绑定到 127.0.0.1:25。启动 smtpd 解决了问题。 @Adam Rosenfield - 是的,这是同一台服务器。 – lugbug 2011-09-08 22:49

Re-posting answer from user that asked the question:

indeed, nothing was bound to 127.0.0.1:25. starting the smtpd solved the problem. @Adam Rosenfield - yes it was the same server. – lugbug Sep 8 '11 at 22:49

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文