user4989939
user4989939

Reputation: 349

Send mail using Python- Error

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']='[email protected]'
msg['To'] = '[email protected]'
s = smtplib.SMTP('localhost')
s.sendmail('[email protected]',['[email protected]'],msg.as_string())
print("Message Sent!")

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

Upvotes: 0

Views: 2897

Answers (4)

Suraj P Patil
Suraj P Patil

Reputation: 76

import smtplib
from smtplib import SMTP       

try:
    sender = '[email protected]'
    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

Upvotes: 0

devautor
devautor

Reputation: 2586

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

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174614

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.

Upvotes: 0

Neil
Neil

Reputation: 332

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.

Upvotes: 0

Related Questions