gpt4 book ai didi

python - sendmail成功,但python smtplib连接失败

转载 作者:太空宇宙 更新时间:2023-11-04 04:27:32 24 4
gpt4 key购买 nike

这有效:

printf 'hi' | sendmail -f myname@example.com myname@example.com

但这失败了:

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 方法正常工作?

最佳答案

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

确实,没有任何内容与 127.0.0.1:25 绑定(bind)。启动 smtpd 解决了问题。 @Adam Rosenfield - 是的,这是同一台服务器。 – lugbug 2011 年 9 月 8 日 22:49

关于python - sendmail成功,但python smtplib连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7353928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com