I\'ve been trying (and failing) to figure out how to send email via Python.
Trying the example from here: http://docs.python.org/library/smtplib.html#smtplib.SMTP
Then I had trie to sent email through smtp.gmail.com, I had the same errors. In my case the Internet provider had close the port 25 (and also 587 and other) for outgoing connections from the IP addresses (from my network) to the external network, leaving open the 25th port only for its mail server. So, at first try:
telnet smtp.gmail.com 587
(587 it your port)
By doing this you can understand, if your port is closed by the Internet provider. You can contact your provider and ask them to open a port for you. My solution was connecting to other network (with open port) Here is the code I used:
gmailaddress = "youremailadress@gmail.com"
gmailpassword = "7777777"
mailto = "youremailadress@gmail.com"
msg = input("What is your message? \n ")
mailServer = smtplib.SMTP('smtp.gmail.com' , 587)
mailServer.starttls()
mailServer.login(gmailaddress , gmailpassword)
mailServer.sendmail(gmailaddress, mailto , msg)
print(" \n Sent!")
mailServer.quit()```
The correct way to connect to GMail using SSL is:
server = smtplib.SMTP('smtp.gmail.com', 587)
Port 465 seems to cause delays. Both ports are specified in a GMail FAQ.
Note that use of port 587 is more common for SMTP over SSL, although this is just trivial information, it has no other practical use.
This answer is provided as community wiki in order to be chosen as "the" answer. Please improve as needed.
The problem is due to a bug in Python. Trying to create a connection with SMTP_SSL will fail with "SMTPServerDisconnected: please run connect() first."
A fix has been committed, so you can patch your local copy. See the attachment named "smtplib_72551.diff".
(Note: SMTP_SSL is a new class added to Python 2.6/3.0 and later.)
Incorrect port maybe? I'm using 587 for smtp.gmail.com and it works.