Python 尝试发送电子邮件时 smt 库出现错误
Python smtlib raising error when trying to send e-mail
我从这里的 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
我寻找过类似的问题,但找不到很多,只找到有人说防火墙可能是问题 --> 我关闭 Avast/firewall,由于某种原因出现蓝屏,重新启动我的PC,Avast/firewall 仍然关闭,但它仍然引发了同样的错误。
我也试过给一个端口值,但我仍然得到同样的错误。可能是什么问题?
此错误最可能的原因是您尝试连接的计算机上没有 SMTP 服务器运行。
server = smtplib.SMTP('192.168.2.4')
'192.168.2.4'
应该是您尝试用来发送电子邮件的 SMTP 服务器的地址。
我从这里的 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
我寻找过类似的问题,但找不到很多,只找到有人说防火墙可能是问题 --> 我关闭 Avast/firewall,由于某种原因出现蓝屏,重新启动我的PC,Avast/firewall 仍然关闭,但它仍然引发了同样的错误。
我也试过给一个端口值,但我仍然得到同样的错误。可能是什么问题?
此错误最可能的原因是您尝试连接的计算机上没有 SMTP 服务器运行。
server = smtplib.SMTP('192.168.2.4')
'192.168.2.4'
应该是您尝试用来发送电子邮件的 SMTP 服务器的地址。