Error when trying to send email using python - python

I am trying to send a mail using python but I keep getting an error
This is the code,
with smtplib.SMTP("smtp.gmail.com", port=587) as connection:
connection.starttls()
connection.login(user=my_email, password=my_pass)
connection.sendmail(
from_addr=my_email,
to_addrs="kesterdan17#gmail.com",
msg=f"Subject:Monday's Juice\n\n{random.choice(quotes)}"
)
And this is the error I am getting,
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

Related

I am getting TimeoutError: [WinError 10060] while sending email from python smtp

I am a beginner in python. i setup a script to send email from gmail using smtp in python.
i got following error
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\socket.py", line 832, 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
i tried to find all solution from stackoverflow and google but failed any solution.
here is what i tried to fix and failed
i generate a new password from app password from gmail setting , i tried to changed port number but failed.
import smtplib
my_email = "myemail#gmail.com"
password = "mypassword"
with smtplib.SMTP("smtp.gmail.com") as connection:
connection.starttls()
connection.login(user=my_email, password=password)
print("logged in")
connection.sendmail(from_addr=my_email, to_addrs="myanotheremail#gmail.com", msg="Hello")

pip._vendor strange behaviour but requests works

I got issue when running my code. I see following error. The interesting thing is when i take this code to other environment like visual code the code is working and i am getting response... I think something is wrong with from pip._vendor import requests which pycharm adding automatically. For instance in visual code is adding imports requests and it works. What should i do this code to run correctly in pycharm?
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
During handling of the above exception, another exception occurred:
self, "Failed to establish a new connection: %s" % e
pip._vendor.urllib3.exceptions.NewConnectionError: <pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000217D0A2B9C8>: Failed to establish a new connection: [WinError 10060] A connection atte
mpt 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
raise MaxRetryError(_pool, url, error or ResponseError(cause))
pip._vendor.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='xxx.xxx', port=443): Max retries exceeded with url: /api/ser (Caused by NewConnectionError('<pip._vend
or.urllib3.connection.HTTPSConnection object at 0x00000217D0A2B9C8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respo
nd after a period of time, or established connection failed because connected host has failed to respond'))
This is my code:
from pip._vendor import requests
if __name__ == '__main__':
print(msg)
data = {
"Ex": 22
}
}
headers = {
"Authorization": "Bearer xxxx"
}
response = requests.post("https://xxx.xxx/api/ser", headers=headers, json=data)
print(response.json())
You are importing the _vendor import of your pip library. It works, but you can just do import requests instead (if it has been already installed).

How do I resolve a connection error when sending an email through outlook with python?

I am trying to send an email with python, because I have to send many emails in bulk and would like to do so with python. The code that I have written is below. When I run this however, I get the following error
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
I think this might be a problem with the setup in Outlook, but I am unsure.
import smtplib
with smtplib.SMTP('smtp.outlook365.com',587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('email#address.com','password')
subject = 'This is a test.'
body = 'wonder if this works!'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail('sender#email.com','receiver#email.com',msg)
Check the address of the smtp server, referring to the Microsoft documentation I saw that the address is different.
SMTP server name smtp.office365.com
https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040

smtplib timing out with correct credentials for office 365

I am creating a python script that uses our company's office 365 email domain to send emails.
However, I cannot establish a connection.
import smtplib
mailserver = smtplib.SMTP('smtp.office365.com',535, timeout=120)
mailserver.ehlo()
mailserver.starttls()
mailserver.login('user#company.com','Password')
mailserver.sendmail('another.user#company.com')
mailserver.quit()
The error I am getting is:
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
I have absolutely no idea where I am going wrong. After reviewing the documentation it my code seems perfect.
Any input is greatly appreciated!
I found the answer for anyone else looking.
I just had to enable SMTP on my O365 email: https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission#:~:text=Open%20the%20Microsoft%20365%20admin,%3D%20disabled%2C%20checked%20%3D%20enabled.
Luckily I had admin access here but you may need to contact your office 365 admin if you can't

socket.error: [Errno 10060] while sending an email with Python

I am learning Python and wanted to try a small python script where I can send email with that script.
My Code:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.login("youremailusername", "password")
msg = "Hello! Test Script For Sending an email"
server.sendmail("you#gmail.com", "target#example.com", msg)
Error 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
Can some one please help me what with problem?
the network you're on might be blocking that port (587) for outgoing connections, or Google might be blocking your IP address. If you're on a VPS, you can check the firewall settings for your system and see if you can open it.

Categories