简体   繁体   中英

Python smtlib raising error when trying to send e-mail

I copied this code right from the smtplib docs over here

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()

However this error is raised:

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

I looked for similar problems but couldn't find a lot, only found someone who said the firewall might be the problem --> I turned Avast/firewall off, got a bluescreen from some reason, restarted my PC, Avast/firewall was still off but still it raised the same error.

I have also tried giving a port value but I still get the same error. What might be the problem?

The most likely reason for this error is that there is no SMTP server running on the computer you are trying to connect to.

server = smtplib.SMTP('192.168.2.4')

'192.168.2.4' should be the address of the SMTP server you are trying to use to send the email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM