我直接从这里的smtplib文档复制了这段代码
import smtplib
def prompt(prompt):
return input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while True:
try:
line = input()
except EOFError:
break
if not line:
break
msg = msg + line
print("Message length is", len(msg))
server = smtplib.SMTP('192.168.2.4')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
但是会出现此错误:
Traceback (most recent call last):
File "C:\Python34\geenemail.py", line 24, in <module>
server = smtplib.SMTP('192.168.2.4')
File "C:\Python34\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python34\lib\smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
self.source_address)
File "C:\Python34\lib\socket.py", line 509, in create_connection
raise err
File "C:\Python34\lib\socket.py", line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd
我找了类似的问题,但找不到很多,只发现有人说防火墙可能是问题 - &gt;我关闭了Avast /防火墙,从某种原因得到了蓝屏,重新启动了我的电脑,Avast /防火墙仍然关闭但仍然引发了同样的错误。
我也试过给出一个端口值,但我仍然得到同样的错误。可能是什么问题?
答案 0 :(得分:1)
此错误的最可能原因是您尝试连接的计算机上没有运行SMTP服务器。
server = smtplib.SMTP('192.168.2.4')
'192.168.2.4'
应该是您尝试用来发送电子邮件的SMTP服务器的地址。