Here is my code:
import smtplib
connection = smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user="mymail#gmail.com", password="mypassowrd")
connection.sendmail(from_addr="mymail#gmail.com", to_addrs="recievermail#gmail.com", msg="Hello")
connection.close()
So I am getting this error:
Traceback (most recent call last):
File "E:\100Days-Python_programs\Day32\Birthday Wisher (Day 32) start\main.py", line 3, in <module>
connection = smtplib.SMTP("smtp.gmail.com")
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [WinError 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
Process finished with exit code 1
I turned on Less secure app access:
I turned off every security steps too:
And I also turned off the firewall protection as well.
But Nothing worked.
So please someone help me.
You need to specify the port. In this case, it's 587 for TSL.
Somehow it works, but I don't have profound knowledge to explain why.
I had the same problem, so there is a solution:
connection = smtplib.SMTP("smtp.gmail.com", 587)
Related
I'm doing a code to send emails automatically using Python and a local server (where I work). I don't know why this error happens.
I've tried to connect the server using commands from module smtplib -> smtplib.SMTP_SSL(hot, port) and smtplib.SMTP(hot,port) but both doesn't work.
import smtplib
server = smtplib.SMTP('IPfromCompanyServer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 727, in create_connection
raise err
File "C:\Users\dbou\AppData\Local\Programs\Python\Python37\lib\socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Make sure you're getting to Mail server IP and port number right. And some clients like Gmail don't allow you to send mails automatically without disabling the secure mail transfer feature.
Additionally you could add this piece of code to your program before you log in.
try:
self.smtp.ehlo()
self.smtp.starttls()
self.smtp.ehlo
except:
print "No TLS "
#login here
And maybe this link outta help out a little:
https://superuser.com/questions/1292420/sending-an-email-from-python-using-local-python-smtp-server
I want to send emails from my Gmail account using python. I followed steps given in this stackoverflow post: How to send an email with Python?
But, my the mails that I sent do not reach the addresses.
This is the error that I get:
Traceback (most recent call last):
File "something.py", line 24, in <module>
server = smtplib.SMTP('myserver')
File "/anaconda2/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/anaconda2/lib/python2.7/smtplib.py", line 317, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/anaconda2/lib/python2.7/smtplib.py", line 292, in _get_socket
return socket.create_connection((host, port), timeout)
File "/anaconda2/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
What should I be doing here?
What you've get is a DNS query error indicating that domain myserver does not exist.
You have to replace the argument myserver in server = smtplib.SMTP('myserver') with the actual address of SMTP server, such as smtp.mail.yahoo.com.
This is how I do it.
import smtplib
server=smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('your_email#gmail.com','your_password')
server.sendmail('your_email#gmail.com','your_email#gmail.com','test 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
Before you mark this as a duplicate; I've read the other threads as well. I'm trying to send an email as simple as using the mail() function in php. I simply tried the example from the official python docs. But when I try to connect with the localhost on my linux machine I get:
>>> import smtplib
>>> smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused
I thought this example didn't need an email server to run (just like the php mail() function doesn't need an email-server to run).
Any ideas what I might be doing wrong or how I can get this to send an email? All tips are welcome!
The php mail() function does need a relay to send messages. It defaults to sendmail on Linux machines.
On Windows you have to give it the address of a SMTP server.
In order for any program to send email, you need to connect to a SMTP server. There is no running around that.
I'm trying to get a connection established to a FTP server with SSL from within Python (v3.3.0). But I keep getting a timeout. I am NOT using port 990 as the SSL port (paranoid). Would that be the cause of this problem? And if so, how do I specify the port I am using?
Here's my script:
from ftplib import FTP
from ftplib import FTP_TLS
ftps = FTP_TLS('ip address')
ftps.auth()
ftps.sendcmd('USER uname')
ftps.sendcmd('PASS password')
ftps.prot_p()
ftps.retrlines('LIST')
ftps.close()
And here is the result:
Traceback (most recent call last):
File "Scrpit name removed for posting", line 12, in <module>
ftps.retrlines('LIST')
File "C:\Python33\lib\ftplib.py", line 767, in retrlines
conn = self.transfercmd(cmd)
File "C:\Python33\lib\ftplib.py", line 381, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python33\lib\ftplib.py", line 742, in ntransfercmd
conn, size = FTP.ntransfercmd(self, cmd, rest)
File "C:\Python33\lib\ftplib.py", line 343, in ntransfercmd
source_address=self.source_address)
File "C:\Python33\lib\socket.py", line 424, in create_connection
raise err
File "C:\Python33\lib\socket.py", line 415, in create_connection
sock.connect(sa)
TimeoutError: [WinError 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
any advice would be greatly appreciated,
After looking at the ftplib source, it doesn't seem to want to use any port but 21.
I think you should be able to work around this, something like
import ftplib
ftplib.FTP.port = 995 # or whatever port you are using
ftps = ftplib.FTP_TLS('hostname', 'user', 'pwd')
ftps.retrlines('LIST')
Set the port through the connect
import ftplib
ftps = ftplib.FTP_TLS()
ftps.connect ('hostname', 991)