Sendmail Errno[61] 连接被拒绝
我一直在尝试让我的应用程序将一些输出的文本邮寄到电子邮件中。为了简化起见,我隔离了脚本:
import smtplib
import sys
import os
SERVER = "localhost"
FROM = os.getlogin()
TO = [raw_input("To : ")]
SUBJECT = "Message From " + os.getlogin()
print "Message : (End with ^D)"
TEXT = ''
while 1:
line = sys.stdin.readline()
if not line:
break
TEXT = TEXT + line
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
该脚本输出:
Traceback (most recent call last):
File "/Users/christianlaustsen/Dropbox/Apps - Python/mail/smtplib_mail.py", line 32, in <module>
server = smtplib.SMTP(SERVER)
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 512, in create_connection
raise error, msg
error: [Errno 61] Connection refused
如您所见,连接被拒绝。我在 Mac OS X Snow Leopard 上运行 Python 2.6(如果相关的话)。
我尝试了很多搜索,但未能找到解决方案。任何帮助将不胜感激。
I've been trying to get my application to mail some outputted text to an email. For simplification I have isolated the script :
import smtplib
import sys
import os
SERVER = "localhost"
FROM = os.getlogin()
TO = [raw_input("To : ")]
SUBJECT = "Message From " + os.getlogin()
print "Message : (End with ^D)"
TEXT = ''
while 1:
line = sys.stdin.readline()
if not line:
break
TEXT = TEXT + line
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()
This script outputs :
Traceback (most recent call last):
File "/Users/christianlaustsen/Dropbox/Apps - Python/mail/smtplib_mail.py", line 32, in <module>
server = smtplib.SMTP(SERVER)
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 512, in create_connection
raise error, msg
error: [Errno 61] Connection refused
So as you can see, the connection is being refused. I'm running Python 2.6 on Mac OS X Snow Leopard (if that's relevant).
I have tried searching around a lot, but haven't been able to find a solution. Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您按如下方式启动本地服务器:
确保修改邮件发送代码以使用非标准端口号:
If you start a local server as follows:
Make sure to modify the mail-sending code to use the non-standard port number:
使用 Python 启动一个简单的 SMTP 服务器,如下所示:
Start a simple SMTP server with Python like so:
我的猜测是您的本地计算机上没有安装任何 SMTP 服务器。
如果您的电子邮件不敏感,请打开 Gmail 帐户并通过 Python 使用它发送电子邮件< /a>.
My guess is that you do not have any SMTP server installed on your local machine.
If your emails are not sensitive, open a Gmail account and send your emails using it with Python.
如果您不想运行单独的服务器,并且只使用 Unix,则可以使用此技术,复制自 http://www.yak.net/fqa/84.html,最初来自 Python FAQ:
在 Unix 上,非常简单,使用 sendmail。 sendmail 程序的位置因系统而异;有时是/usr/lib/sendmail,有时是/usr/sbin/sendmail。 sendmail 手册页将为您提供帮助。这是一些示例代码:
If you don't want to run a separate server, and if you're only using Unix, you can use this technique, copied from http://www.yak.net/fqa/84.html, and originally from the Python FAQ:
On Unix, it's very simple, using sendmail. The location of the sendmail program varies between systems; sometimes it is /usr/lib/sendmail, sometime /usr/sbin/sendmail. The sendmail manual page will help you out. Here's some sample code:
我想创建一些东西,这样你就可以复制粘贴它并使其工作,但这是我得到的最接近的:
我觉得该教程具有误导性,因为它假设没有很好地告诉你你已经有一个正在运行的服务器发送电子邮件给你的邮件...这很奇怪。我的脚本的唯一问题是,如果没有刚刚写在那里的明文密码,我不知道如何使其工作,但是唉......至少它发送了它?只需制作一个假电子邮件地址或其他东西...
很久以前就提出了这个问题,所以我不记得这到底意味着什么,将其放在这里以防万一:
我可能只是使用假电子邮件来解决这个问题,或者类似的事情,或者来自我的组织的电子邮件不记得了。祝你好运!
I wanted to create something so that you could just copy paste it and have it work but this is the closest I got:
I feel the tutorial is misleading because it assumes without telling you very well that you already have a running server that sends e-mails for you...its odd. The only issue with my script is that I dont know how to make it work without having the cleartext password just written there but alas...at least it sends it? Just make a fake e-mail address or something...
made this question long time ago, so I don't remember what this means exactly put will put it here just in case:
I probably went around it by using a fake email only for that or somehting like that or an email from my org can't remember. Good luck!
如果您是系统的 root 用户,那么您可能需要安装
opensmtpd
。首先,您不需要手动运行服务器(默认情况下启用此服务,因此在安装smtpd
之后,可以手动启动它或重新启动计算机)。其次,您不需要更改server = smtplib.SMTP(SERVER)
行。最后,使用 yum install opensmtpd 或等效的 apt-get 命令。If you are root on your system then you may want to install
opensmtpd
. First this way you don't need to run the server manually (this service is enabled by default so aftersmtpd
installation either start it manually or reboot your machine). Second, you don't need to change the lineserver = smtplib.SMTP(SERVER)
. To conclude, useyum install opensmtpd
or the equivalentapt-get
command.无论出于何种原因,我都很难将服务器和端口传递给构造函数,但无法将连接函数传递给构造函数。这最终对我有用:
For whatever reason, I had difficulty passing server and port to the constructor, but not the connect function. This ended up working for me: