Ravi Singh
Ravi Singh

Reputation: 315

How to send a email by using smtplib properly?

I wrote a code to send email using smtplib and gives me error that 'connection refused'

This is the error traceback

Traceback (most recent call last):
  File "mail.py", line 20, in <module>
    s = smtplib.SMTP('localhost')
  File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

python file

    import smtplib

    from email.message import EmailMessage

    msg = EmailMessage()
    msg.set_content('Hello, This message is sent by using python 
    smtplib')

    msg['Subject'] = 'Use of Smtp'
    msg['From'] = '[email protected]'
    msg['To'] = '[email protected]'

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

Upvotes: 0

Views: 840

Answers (1)

Rangeesh
Rangeesh

Reputation: 363

if you want to send mail to gmail you have to modify gmail development settings. refer below for more info: https://realpython.com/python-send-email/

Upvotes: 1

Related Questions