0

I am just trying the email module, and I cannot get even the simplest part of it to work! I am using the first example from the email module docs, here. When I run this program:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
with open('email.txt') as fp:
    # Create a text/plain message
    msg = MIMEText(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Subject'
msg['From'] = 'user@example.com'
msg['To'] = 'otheruser@example.com'

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

I get this error message:

Traceback (most recent call last):
  File "C:/Users/User/emailTest.py", line 20, in <module>
    s = smtplib.SMTP('localhost')
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 335, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\smtplib.py", line 306, in _get_socket
    self.source_address)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socket.py", line 711, in create_connection
    raise err
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\socket.py", line 702, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
nedla2004
  • 1,115
  • 3
  • 14
  • 29
  • Do you actually have a mail server on your local machine? If you don't, this is correct behaviour as there is nothing to connect to. – Ken Y-N Nov 11 '16 at 03:40
  • @KenY-N I do not, I was under the impression that this would work without one, but I guess that is incorrect. Is there a simple way of sending an email without a mail server? – nedla2004 Nov 11 '16 at 03:43
  • You would have to use an external one; your employer's, your ISP's, or some other third party, or if you want to do it locally, try [the WAMP stack](http://stackoverflow.com/q/16830673/1270789). – Ken Y-N Nov 11 '16 at 03:45

0 Answers0