dharmista
dharmista

Reputation: 103

error sending mail in python

pwd = "mypassword"
import smtplib

server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("[email protected]",pwd)

msg = "YOUR MESSAGE!"
server.sendmail("[email protected]", "[email protected]", msg)
server.quit()

I have tried sending a mail through python...
Error :

Traceback (most recent call last):
  File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
    server = smtplib.SMTP('smtp.gmail.com')
  File "C:\Python27\lib\smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 316, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "C:\Python27\lib\socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

I am connecting through a proxy connection
I set the proxy through cmd in windows. Please help me with this.

Update :
I am sure with the internet connection :

import urllib2

def internet_on():
    try:
        response=urllib2.urlopen('https://www.google.co.in',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

print internet_on()

Output is True

Upvotes: 3

Views: 13821

Answers (2)

KarolisR
KarolisR

Reputation: 626

Your code works fine for me, so it's probably the connection settings.

Try changing server to:

server = smtplib.SMTP('64.233.184.108')

(that's the IP address of smtp.gmail.com, to bypass DNS resolution)

Upvotes: 7

JustDucky
JustDucky

Reputation: 132

Try updating the

server = smtplib.SMTP('smtp.gmail.com')
to include a port. The port for gmail is 587, so add that as a second parameter:
server = smtplib.SMTP('smtp.gmail.com', 587)

If this does not work, make sure that you have used this site to make sure that the account will let you in:

https://www.google.com/settings/security/lesssecureapps

Upvotes: 0

Related Questions