Sending email via gmail via python app

as indicated in the title. I am having problem sending email through my gmail account via python app. I searched the internet for a solution but nothing seems to be resolved and I thought I could ask here. My code is as follows:

FROMADDR = "myemail@gmail.com"
LOGIN    = FROMADDR
PASSWORD = "mypass"
TOADDRS  = "varis81@hotmail.com"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"

      

I get the message:

socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

      

I tried different ports but it doesn't work either. I've tried hotmail as well, but it causes the same problem. I'm using Python 2.7 (don't ask :)) on a windows 7 machine developing on Eclipse with PyDev.

Any help would be great! Thank you in advance.

+3


source to share


2 answers


Ok, since I can't write comments, I'll have to try and answer.



Judging by this: Python SMTP Errno 10060 Perhaps a timeout will help?

+1


source


I am using the same design on one of my servers. My code is below.

The only difference is the extra .ehlo()

after '.starttls ()'. This shouldn't be a problem; from RFC :

5.2 Result of the STARTTLS Command

The client SHOULD send an EHLO command as the first
command after a successful TLS negotiation.

      

According to the RFC, the server should not drop the connection if the client doesn't send ehlo

after starttls

, but Google may be more restrictive on their SMTP server. I would check this first. (I've seen ISPs tighten up conditions of this kind to reduce spam, such as Mailinator 2007 write.)



It can also be a filtered port - try running the code in the REPL and confirm which line is the exception, if so connect()

, you will find out on the net. If it does, your use is probablysmtplib.

Note I also experienced random unclean shutdowns, resulting in tried / excluded .close()

.

import smtplib
s = smtplib.SMTP()
s.connect("smtp.gmail.com")
s.ehlo()
s.starttls()
s.ehlo()
s.login("from@gmail.com", "frompass") 
s.sendmail("fromname@gmail.com", toAddr, bytes)
try:
    s.close()
except: pass

      

+1


source







All Articles