Python SMTP Errno 10060 - python

Can someone give me some insight as to why both this:
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
and this:
mailServer = smtplib.SMTP('smtp.gmail.com:587')
Are saying this:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\socket.py", line 571, in create_connection
raise err
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'm in China.
I have a VPN. I tried it with and without the VPN.
I have, many times before, used this code to send e-mails from my g-mail account. Suddenly today, I got this error.
Gmail, if accessed through my chrome browser, works fine. Albeit, it's taking an unusually long time to send simple e-mails...hmmm

Try specifying a timeout (requires Python 2.6+):
smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
or try connecting via SSL instead of TLS/STARTTLS (with and without timeout):
smtplib.SMTP_SSL("smtp.gmail.com", 465)

I had a similar problem. The call to mailServer = smtplib.SMTP("smtp.gmail.com", 587) just hang there, without doing anything. I can't even quit the program. It stays in the background forever!
But adding the timeout parameter solved the problem! It returns immediately and I can sent email through gmail SMTP server successfully!
Not sure why though!

Look my friend, see the line
mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
and put YOUR_USERNAME after the 587 number, like this
mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)

Related

Getting SMTPServerDisconnected error sending email through python

When trying to send the email with the host:cpanel.freehosting.com
P it is raising an error like
This is my code:
import smtplib
s = smtplib.SMTP('cpanel.freehosting.com', 465)
s.starttls()
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
This is the error i got:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 337, in connect
(code, msg) = self.getreply()
File "/usr/lib/python3.5/smtplib.py", line 393, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Considering the port number you are using I'd try with SMTP_SSL instead of SMTP and starttls().
https://docs.python.org/3/library/smtplib.html:
An SMTP_SSL instance behaves exactly the same as instances of SMTP.
SMTP_SSL should be used for situations where SSL is required from the
beginning of the connection and using starttls() is not appropriate.
If host is not specified, the local host is used. If port is zero, the
standard SMTP-over-SSL port (465) is used.
STARTTLS is a form of opportunistic TLS, it is supposed to be used with old protocols, that originally did't support TLS, to upgrade the connection.
The port 465 was used before the introduction of STARTTLS for SMTPS, which is now deprecated.
import smtplib
s = smtplib.SMTP_SSL('cpanel.freehosting.com', 465)
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
Alternatively you should be able to use port 25 with your original code.
import smtplib
s = smtplib.SMTP('cpanel.freehosting.com', 25)
s.starttls()
s.login("myusername", "mypassword")
message = "Message_you_need_to_send"
s.sendmail("myemailid", "receiver_email_id", message)
s.quit()
In both examples you can completely omit the port number as you are using the default ports.

python- error sending an email

I am able to send from one gmail account to another gmail account and from one outlook account to another outlook account.
But I am facing problem when I am trying to send mail from outlook to gmail.
Here is the code
import smtplib
content ='example email stuff here'
email='xyz#outlook.com'
password="password"
fromemail="xyz#outlook.com"
receive="abc#gmail.com"
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
mail.ehlo()
mail.starttls()
mail.login(email,password)
mail.sendmail(fromemail,receive,content)
mail.close()
When I am running the script I am getting the following error
Traceback (most recent call last):
File "smtp_google.py", line 7, in <module>
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
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 575, in create_connection
raise err
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
Solution 1:
Try by giving timeout in:
mail=smtplib.SMTP('smtp-mail.outlook.com',587)
as
mail=smtplib.SMTP('smtp-mail.outlook.com',587, timeout=120)
or try connecting via SSL instead of TLS/STARTTLS
mail=smtplib.SMTP_SSL('smtp-mail.outlook.com',587)
solution 2:
Your email account must allow smtp, which is not necessarily enabled by default. You can try to change the settings by following the steps given here enable smtp authentication

error sending mail in python

pwd = "mypassword"
import smtplib
server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113#gmail.com",pwd)
msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113#gmail.com", "sender#xyz.com", 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
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)
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

Send email from server python

I am trying to send an email form my server with python, I asked my server provider what port to use for this and they said "choose SMTP Ports 465 (Secure SSL/TLS outgoing server) , 25 ( Non-SSL outgoing server)." Not sure what this exactly means but currently I am using 25, here is my code
#! /usr/bin/python
import smtplib
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
I filled in my username (which is my email address right) and password,a dn the target but it is not working, when I try to navigate to the url where my py script is, it just doesn't load. I have an internet connection cause I am loving other things, and go to other pages on my server. I have also tried running with cron jobs but that also doesn't work.
The permissions on the script are 0755, is there a problem with the script?
When i ran with cron jobs here is the traceback
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
server = smtplib.SMTP('smtp.gmail.com', 25)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
Thanks for the help in advance.
EDIT
here is updated error log with the port at 587
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
EDIT 2
When I had server = smtplib.SMTP('telnet smtp.gmail.com', 587)
I got this error
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
server = smtplib.SMTP('telnet smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
BUG:
First of all you are using gmail server not your company server to send mail .For gmail server the output port is 587
The code:
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
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
Updated
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("ABC#gmail.com", "password")
mail.sendmail("ABC#gmail.com", "recivermailaddress", content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
In python, please download yagmail (disclaimer: I'm the developer):
pip install yagmail
It is then simply a matter of:
import yagmail
yag = yagmail.SMTP("you#gmail.com", "password")
yag.send("target#example.com", 'This is the subject', 'Hello!')
I guess it mostly just has to do with the fact that you gave a wrong port (smtp.gmail.com is not at 25).
On the github you can also see a common list of errors.

Error during sending mail

I try to send mail via python. I got a function:
def mail():
fromaddr = 'mymail#gmail.com'
toaddrs = 'targetmail#o2.pl'
msg = 'There was a terrible error that occured and I wanted you to know!'
# Credentials (if needed)
username = 'mymail#gmail.com'
password = 'password'
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
When I try to execute this function it whows me WinError 10060. Error text below:
Traceback (most recent call last):
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 75, in <module> mail()
File "C:\python-factory\ServiceChecker\main\mainBlock.py", line 65, in mail server = smtplib.SMTP('smtp.gmail.com:587')
File "C:\Python34\Lib\smtplib.py", line 242, in __init__ (code, msg) = self.connect(host, port)
File "C:\Python34\Lib\smtplib.py", line 321, in connect self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\Lib\smtplib.py", line 292, in _get_socket self.source_address)
File "C:\Python34\Lib\socket.py", line 509, in create_connection raise err
File "C:\Python34\Lib\socket.py", line 500, in create_connection sock.connect(sa)
TimeoutError: [WinError 10060]
I don't know where is problem... i tried many mail servers (gmail,o2,wp,) all of them returned the same error. I tried also other available solutions in the internet to send mail via pyython.... the same problem...
Please help
According to your posted backtrace, you are failing at the line
server = smtplib.SMTP('smtp.gmail.com:587')
I've run that line and it works for me. The clue is in the TimeoutError. I believe you are having networking problems and the code itself is fine.
You do have an extra call to ehlo but as it stands you are not getting that far.
Additionally, it would probably be better to pass the port number as a separate argument, rather than using a colon to split it. E.g.
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
The documentation for smptlib is here: https://docs.python.org/2/library/smtplib.html
Try removing the second server.ehlo() . The one just before login. That worked for me.
toaddrs should be a list
toaddrs = ['targetmail#o2.pl']

Categories

Resources