socket.error: [Errno 10060] while sending an email with Python - 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.

Related

Error when trying to send email using 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

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")

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

Connect to a host that doesn't answer to ping

I am trying to connect to a smtp port to automatically send emails through it. The problem is that when trying to connect to it with python smtplib it gives an error that it can't connect to the host. When trying to run nmap with the command
nmap -T4 -p 25,587,465
I get the following output:
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 2.12 seconds
When I add the -Pn option I get the following output:
Host is up.
PORT STATE SERVICE
25/tcp filtered smtp
465/tcp filtered smtps
587/tcp filtered submission
The python script that I have used is:
import smtplib, ssl
port = 465
password = "password"
email = "my#mail"
hostname = "hostname"
context = ssl.create_default_context()
with smtplib.SMTP_SSL(hostname, port, context=context) as server:
server.login(email, password)
And this is the error I get:
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 am running the code on a Windows 10 machine and using Python 3.7.5.
Is there any way for me to connect to one of the email ports and send emails through it through python or c/c++?

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

Categories