1

I have been trying to mail using python SMTPlib, but I am constantly getting the following error :

Traceback (most recent call last):
  File "mailing.py", line 2, in <module>
server = smtplib.SMTP('mail.students.iitmandi.ac.in:587')
  File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
  File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.5/smtplib.py", line 306, in _get_socket
self.source_address)
  File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
  File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
  ConnectionRefusedError: [Errno 111] Connection refused

I am new to python and have only tried mailing from PHP before. Please excuse my lack of knowledge on the topic. My python code is :

import smtplib
server = smtplib.SMTP('mail.students.iitmandi.ac.in:587')
server.starttls()
server.login("username","zxxxxx")

header = 'From: from_addr'
header += 'To: to_addr'
header += 'Subject:'

message = "bmnbjbj"

message = header + message
server.sendmail("from_addr","to_addr",message)

I am sure of my SMTP server details because I am using the same configuration in my Gmail app to communicate using this email.

unkemptArc99
  • 39
  • 1
  • 5

1 Answers1

-1
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: cary@ratatosk.org\n")
p.write("Subject: test\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write("some more text\n")
sts = p.close()
if sts != 0:
    print "Sendmail exit status", sts

Try sending it like this. From the other SO page in the comments

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44