0

I quite new in programing in python. When i try to send an e-mail using python 2.7, i get an error:

from email.mime.text import MIMEText
import smtplib

msg = MIMEText("Hello There!")
msg['Subject'] = 'A Test Message'
msg['From']='kolsason7@walla.com'
msg['To'] = 'yaron148@gmail.com'
s = smtplib.SMTP('localhost')
s.sendmail('kolsason7@walla.com',['yaron148@gmail.com'],msg.as_string())
print("Message Sent!")

  File "C:\Python27\ArcGISx6410.3\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 10061] 
>>> 
user4989939
  • 349
  • 1
  • 3
  • 14

4 Answers4

1

Quoting this from here

It's because you haven't opened the port you are trying to connect to, nothing is listening there. If you're trying to connect to a web or ftp server, start it first. If you're trying to connect to another port, you need to write a server application too.

And see a similar solved problem here

Community
  • 1
  • 1
devautor
  • 2,506
  • 4
  • 21
  • 31
0

Your smtp server is set as localhost, are you sure it's right ? The error is a "connection opening". You may have to find a username/password/address/port combination for the SMTP server that you are using.

Neil
  • 332
  • 2
  • 15
0

The code snippet you have used is not designed to be run on Windows, it is designed to be run on Linux where there is (usually) a service listening on port 25 at localhost.

For Windows, you'll need to connect to an actual mail server before you can send messages.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0
import smtplib
from smtplib import SMTP       

try:
    sender = 'xxx@gmail.com'
    receivers = ['xxx.com']

    message = """ this message sending from python
    for testing purpose
    """
    smtpObj = smtplib.SMTP(host='smtp.gmail.com', port=587)
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.ehlo()
    smtpObj.login('xxx','xxx')
    smtpObj.sendmail(sender, receivers, message)
    smtpObj.quit()
    print "Successfully sent email"
    except smtplib.SMTPException,error:
    print str(error)
    print "Error: unable to send email"

If u ran this code u would see a error message like this stating that google is not allowing u to login via code

Things to change in gmail:

1.Login to gmail

2.Go to this link https://www.google.com/settings/security/lesssecureapps

3.Click enable then retry the code

Hopes it help :)

But there are security threats if u enable it

Suraj P Patil
  • 76
  • 1
  • 8