【问题标题】:Connection Refused Error in SMTPlib executionSMTPlib 执行中的连接被拒绝错误
【发布时间】:2017-12-21 11:34:30
【问题描述】:

我一直在尝试使用 python SMTPlib 发送邮件,但我不断收到以下错误:

Traceback (most recent call last):
  File "mailing.py", line 2, in <module>
server = smtplib.SMTP('mail.students.iitmandi.ac.in:587')
  File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
  File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.5/smtplib.py", line 306, in _get_socket
self.source_address)
  File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
  ConnectionRefusedError: [Errno 111] Connection refused

我是 python 新手,之前只尝试过从 PHP 发送邮件。请原谅我对这个话题缺乏了解。我的python代码是:

import smtplib
server = smtplib.SMTP('mail.students.iitmandi.ac.in:587')
server.starttls()
server.login("username","zxxxxx")

header = 'From: from_addr'
header += 'To: to_addr'
header += 'Subject:'

message = "bmnbjbj"

message = header + message
server.sendmail("from_addr","to_addr",message)

我确定我的 SMTP 服务器详细信息,因为我在我的 Gmail 应用程序中使用相同的配置来使用此电子邮件进行通信。

【问题讨论】:

标签: python email smtplib


【解决方案1】:
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: cary@ratatosk.org\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

尝试像这样发送它。从 cmets 中的另一个 SO 页面

【讨论】:

    猜你喜欢
    • 2017-07-01
    • 2018-08-20
    • 1970-01-01
    • 2018-03-18
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    相关资源
    最近更新 更多