Sendmail Errno[61] 连接被拒绝

发布于 2024-10-31 16:30:38 字数 1560 浏览 5 评论 0原文

我一直在尝试让我的应用程序将一些输出的文本邮寄到电子邮件中。为了简化起见,我隔离了脚本:

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 技术交流群。

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

发布评论

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

评论(7

傲娇萝莉攻 2024-11-07 16:30:38

如果您按如下方式启动本地服务器:

python -m smtpd -n -c 调试服务器本地主机:1025

确保修改邮件发送代码以使用非标准端口号:

server = smtplib.SMTP(SERVER, 1025)
server.sendmail(FROM, TO, message)
server.quit()

If you start a local server as follows:

python -m smtpd -n -c DebuggingServer localhost:1025

Make sure to modify the mail-sending code to use the non-standard port number:

server = smtplib.SMTP(SERVER, 1025)
server.sendmail(FROM, TO, message)
server.quit()
新一帅帅 2024-11-07 16:30:38

使用 Python 启动一个简单的 SMTP 服务器,如下所示:

python -m smtpd -n -c DebuggingServer localhost:1025

Start a simple SMTP server with Python like so:

python -m smtpd -n -c DebuggingServer localhost:1025
奢华的一滴泪 2024-11-07 16:30:38

我的猜测是您的本地计算机上没有安装任何 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.

一枫情书 2024-11-07 16:30:38

如果您不想运行单独的服务器,并且只使用 Unix,则可以使用此技术,复制自 http://www.yak.net/fqa/84.html,最初来自 Python FAQ:

在 Unix 上,非常简单,使用 sendmail。 sendmail 程序的位置因系统而异;有时是/usr/lib/sendmail,有时是/usr/sbin/sendmail。 sendmail 手册页将为您提供帮助。这是一些示例代码:

SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: [email protected]\n")
p.write("Subject: test\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write("some more text\n")
sts = p.close()
if sts != 0:
    print "Sendmail exit status", sts

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:

SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: [email protected]\n")
p.write("Subject: test\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write("some more text\n")
sts = p.close()
if sts != 0:
    print "Sendmail exit status", sts
缱绻入梦 2024-11-07 16:30:38

我想创建一些东西,这样你就可以复制粘贴它并使其工作,但这是我得到的最接近的:

from email.message import EmailMessage
import smtplib
import os

def send_email(message,destination):
    # important, you need to send it to a server that knows how to send e-mails for you
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    # don't know how to do it without cleartexting the password and not relying on some json file that you dont git control...
    server.login('[email protected]', 'password_for_gmail')
    msg = EmailMessage()
    msg.set_content(message)

    msg['Subject'] = 'TEST'
    msg['From'] = '[email protected]'
    msg['To'] = destination
    server.send_message(msg)

if __name__ == '__main__':
    send_email('msg','destination@email')

我觉得该教程具有误导性,因为它假设没有很好地告诉你你已经有一个正在运行的服务器发送电子邮件给你的邮件...这很奇怪。我的脚本的唯一问题是,如果没有刚刚写在那里的明文密码,我不知道如何使其工作,但是唉......至少它发送了它?只需制作一个假电子邮件地址或其他东西...


很久以前就提出了这个问题,所以我不记得这到底意味着什么,将其放在这里以防万一:

仅当您启用对安全性较低的应用的访问权限时,此功能才有效:myaccount.google.com/lesssecureapps。我认为你应该回答这个问题。

我可能只是使用假电子邮件来解决这个问题,或者类似的事情,或者来自我的组织的电子邮件不记得了。祝你好运!

I wanted to create something so that you could just copy paste it and have it work but this is the closest I got:

from email.message import EmailMessage
import smtplib
import os

def send_email(message,destination):
    # important, you need to send it to a server that knows how to send e-mails for you
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    # don't know how to do it without cleartexting the password and not relying on some json file that you dont git control...
    server.login('[email protected]', 'password_for_gmail')
    msg = EmailMessage()
    msg.set_content(message)

    msg['Subject'] = 'TEST'
    msg['From'] = '[email protected]'
    msg['To'] = destination
    server.send_message(msg)

if __name__ == '__main__':
    send_email('msg','destination@email')

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:

It works only if you enable access for less secure apps: myaccount.google.com/lesssecureapps . I think you should put that in answer.

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!

时光是把杀猪刀 2024-11-07 16:30:38

如果您是系统的 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 after smtpd installation either start it manually or reboot your machine). Second, you don't need to change the line server = smtplib.SMTP(SERVER). To conclude, use yum install opensmtpd or the equivalent apt-get command.

小忆控 2024-11-07 16:30:38

无论出于何种原因,我都很难将服务器和端口传递给构造函数,但无法将连接函数传递给构造函数。这最终对我有用:

    s = smtplib.SMTP(timeout=30) #seconds
    s.connect(EMAIL_HOST, EMAIL_PORT)
    m = MIMEText('see subject')
    m['subject'] = 'sweet subject'
    m['from'] = EMAIL_FROM
    m['to'] = to_list  # comma-separated list of emails
    s.sendmail(m['from'], m['to'].split(','), m.as_string())
    s.quit()

For whatever reason, I had difficulty passing server and port to the constructor, but not the connect function. This ended up working for me:

    s = smtplib.SMTP(timeout=30) #seconds
    s.connect(EMAIL_HOST, EMAIL_PORT)
    m = MIMEText('see subject')
    m['subject'] = 'sweet subject'
    m['from'] = EMAIL_FROM
    m['to'] = to_list  # comma-separated list of emails
    s.sendmail(m['from'], m['to'].split(','), m.as_string())
    s.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文