Unable to run a simple SMTP server - python

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
import smtplib
senders='jsujith1311#gmail.com'
password="ekjhgisxavzw"#Not Original Password
connection=smtplib.SMTP("smtp.gmail.com")
connection.starttls()
connection.login(user=senders,password=password)
connection.sendmail(from_addr=senders,to_addrs=senders,msg="HI")
connection.close()
Can anyone help with this
I need a simple smtp server running on my device initially, then i want to configutre it according to my needs.

You need to change your password to an app password. Follow the below link for that: https://support.google.com/mail/answer/185833?hl=en

So, the answer is to use
connection = smtplib.SMTP_SSL("smtp.gmail.com")
and we have to remove the tls connection,I don't know why.But it worked.

Related

Python API client for Elastic Search: can't connect due to Connection timed out

I am working with the Python API client for Elastic Search and I am trying to connect to it.
I create the client like this:
def setup_es():
ES_USER = os.getenv("ES_USER")
ES_PASS = os.getenv("ES_PASS")
print(f"Setting up ES with HOST={ES_HOST}, USER={ES_USER}, PASS={ES_PASS}")
return Elasticsearch([ES_HOST], basic_auth=(ES_USER, ES_PASS))
But whenever I try using the client, I always get a connection timed out error.
For example:
client.info()
or
client.options(ignore_status=[400,404]).indices.delete(index=MY_INDEX)
Always produce:
***_transport.ConnectionTimeout: Connection timed out
I know the host, user and password are right. Am I missing something else? Any ideas please?
I solved this issue by specifying port 443 in the elastic search host variable.

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

Errno 10060 A connection attempt failed

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=465
EMAIL_HOST_USER = 'yogi'
EMAIL_HOST_PASSWORD = '###'
DEFAULT_EMAIL_FROM = 'yogi#gmail.com'
above are the settings for django core mail module. I am using its send_mail to send mails to users. When i try to build the program with the gmail smtp it throws the following 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 am doing this in my company and so it has proxy settings. I have given the proxy credentials in .condarc settings file. But still the connection timeout error. Do i need to set the proxy settings somewhere else or let me know where i am going wrong. ?
As far as I know django does not detect any SMTP proxy settings from anaconda configuration files. You can overcome this by manually building a connection.
Notice that send_mail , has an option parameter for a connection. You get one by calling mail.get_connection now you need to wrap it around sockspi
see Python smtplib proxy support and Python send email behind a proxy server for further details.

pymsql not connecting to a database

I am trying to make software that works on a large number of people's computers by connecting to a login server. I have set up a MSQL server using 24hosting and added a database. I then tried to access the database using python, but it gives me the error "No connection could be made because the target machine actively refused it".
I need everyone who downloads this program to be able to connect, not just this computer.
This is probably something to do with my server, and not to do with code, but I will post the code below anyway.
from os import getenv
import pymysql
server = getenv("31.220.17.13")
user = getenv("shutdow1_user")
password = getenv("DSAEWQ321")
conn = pymysql.connect(server, user , password, "tempdp")
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
cur.close()
conn.close()
Try to run tcpdump or tshark host 31.220.17.13 to see what exactly happens.
Most likely TCP connection to mysql port is filtered (you would see RST replies on SYN send), or if you see that connection is closed by remote party after TCP session is established - that would be a sign of remote mysql server not configured properly to accept client connection for your IP/username/password/database.

closing a socks/proxy connection in python

I am writing some code that uses poplib and imaplib to collect emails through a proxy server.
I use the following to set up a proxy connection:-
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket
Which I got from the stackoverflow post:-
http://stackoverflow.com/questions/3386724/python-how-can-i-fetch-emails-via-pop-or-imap-through-a-proxy
Then I make my connection with the email server:-
server = poplib.POP3(self.host, self.port)
server.user(self.username)
server.pass_(self.password)
I am testing my code in a unittest and have encountered a problem that I believe relates to my connection with the proxy not closing down properly.
An example is:-
I have set up the proxy connection and am trying to establish a connection with the email server. As part of the unittest I intentionally use an incorrect email server password.
The poplib library throws an exception that it can't connect. I catch the exception in the unittest, then move on to the next unittest, trusting the poplib library would properly close my previous connection.
My understanding is that this is not a good thing and that I should be ensuring the email and proxy server connections are properly closed.
I know how to close the pop3 connection:-
server.quit()
But do not know how to close the connection with the proxy server or if I have to do so.
Could someone please help me with this question or with my understanding if that's where the problem lies :)
No special action is required. When you close the POP connection, the proxy connection will close automatically, since it's only needed while you are connected to something through the proxy.

Categories