Search code examples
pythonemailgmail

Errno 10060 python sending email through gmail


I'm trying to send an email through a gmail account using python 2.7. My code is below, any help is appreciated! I keep getting:

Errno 10060- connection attempt failed because connected party did not properly respond after a period of time...

import smtplib

FROMADDR = "[email protected]"
LOGIN    = FROMADDR
PASSWORD = "mypassword"
TOADDRS  = "[email protected]"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 25, timeout=120)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"

Solution

  • It is timing out because you are connecting on the port for email routing between servers instead of the mail submission agent port. It worked fine for me when using port 587:

    >>> import smtplib
    >>> server = smtplib.SMTP('smtp.gmail.com',587)
    >>> server.ehlo()
    (250, 'mx.google.com at your service, [99.178.174.213]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES')
    >>> server.starttls()
    (220, '2.0.0 Ready to start TLS')
    >>> server.ehlo
    <bound method SMTP.ehlo of <smtplib.SMTP instance at 0x1e518c0>>
    >>> server.login('xxxxxxxxxxxx','xxxxxxxx')
    (235, '2.7.0 Accepted')
    >>> msg = "test message"
    >>> server.sendmail('[email protected]','[email protected]',msg)
    {}
    >>> server.quit()
    (221, '2.0.0 closing connection xxxxxxxxxxxxx.x - gsmtp')