Python IMAPlib ErrNo 10060 - python

I'm writing a script to grab the link from an email that my web application sends out to then start off a set of automated tests. At the minute my code should only grab the body of the email, I haven't started to scan for the link within that yet because I get no return.
import imaplib
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('user', 'passwd')
mail.list()
mail.select("inbox")
result, data = mail.search(None, "ALL")
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
print raw_email
I don't see anything wrong in my code for getting the newest email (top of the inbox) and my IMAP is set up for the account. Can anyone enlighten me as to why I'm getting the error:
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
Thanks in advance.
Edit: Company firewall is blocking port 143 and 993 (default IMAP(S) port), I've posted my workaround as the answer. There is nothing wrong with my code!

To get around the company port restrictions I used the netsh command to port forward an open port to port 993 (default for imaplib's SSL) using the following command in terminal
netsh interface portproxy add v4tov4 listenport=UNBLOCKTHISPORT listenaddress=YOURIP connectport=THISPORTISOPEN connectaddress=YOURIP

Related

How to verify an email in python without sending an email by verifying records?

import smtplib, ssl
import dns.resolver,socket
smtp_server= 'smtp.gmail.com'
port = 465
sender = 'reddy#gmail.com'
pssword = ''
addressToVerify = 'raghavareddy#smatbot.com'
context = ssl.create_default_context()
domain_name = addressToVerify.split('#')[1]
print('domain_name : '+domain_name)
records = dns.resolver.resolve(domain_name, 'MX')
mxRecord = records[0].exchange
print(mxRecord)
host = socket.gethostname()
print('hosttttttttttt==',host)
try:
server = smtplib.SMTP_SSL(smtp_server,port)
server.ehlo()
#server.starttls(context = context)
server.ehlo()
server.login(sender,pssword)
ss = server.connect(str(mxRecord),465)
#print(ss)
pp = server.verify(addressToVerify)
print(pp)
ss = server.rcpt(addressToVerify)
print(ss)
print('ok')
except Exception as e:
print(e)
I was trying to verify the email address but it was throwing an error while it was not connecting mxrecords
[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
It is not connecting to the MX records.
while verifying at
RCPT (503, b'5.5.1 MAIL first. p2-20020a17090ad30200b001cd4989feb7sm14376983pju.3 - gsmtp')
This 503 error code representing that `The server has encountered a bad sequence of commands, or it requires an authentication.
Suggest a standard way of verifying an email, please.
Suggest a standard way of verifying an email, please.
You can not, without sending an email with a nonce, and waiting for it to be used in some timeframe.
Servers won't let you arbitrarily test the existence of email addresses, otherwise this would be an heaven for spammers.

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++?

Connect to SMTP (SSL or TLS) using Python

I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. Only the use of sockets is allowed (so not the smtplib). I need to: send HELO command, MAIL FROM, RCPT TO, and DATA.
There are many cases of similar problems posted, but they haven't received the proper answer. For example:
Implementing Transport Layer Security in Python - Simple Mail Client
The program is required to connect to smtp.gmail.com over port 587. I've taken two different approaches:
Using STARTTLS:
mailserver = 'smtp.gmail.com'
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024)
print recv
if recv[:3] != '220':
print '220 reply not received from server.'
#Send HELO command and print server response
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
print recv1
if recv1[:3] != '250':
print '250 reply not received from server.'
#Send MAIL FROM command and print server response.
command = "STARTTLS\r\n"
clientSocket.send(command)
recvdiscard = clientSocket.recv(1024)
print recvdiscard
clientSocket.send("MAIL From: email\r\n")
recv2 = clientSocket.recv(1024)
print recv2
if recv2[:3] != '250':
print '250 reply not received from server.'
Using SSL:
clientSocketSSL = ssl.wrap_socket(clientSocket)
Then clientSocketSSL replaces all instances of clientSocket. The STARTTLS lines are also removed and import ssl is added to the top.
When using the first method, the MAIL FROM: command isn't returning anything. I'm getting the following output:
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
250 reply not received from server.
When using SSL, I'm getting the same as the linked post:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Am I missing something here? I guess my best option is to use TLS but I have no idea how to go about that... is there something wrong with my MAIL FROM command?
When using SSL, you need to connect to port 465 instead of port 587. If you use STARTTLS, you still need to use ssl.wrap_socket, you just do it later - specifically, after receiving the 220 response to the STARTTLS command. After doing STARTTLS, you're supposed to do HELO again, since the server is supposed to forget anything that happened before the STARTTLS.
In either case, the servers at smtp.google.com ports 465 and 587 still won't return a 250 response to the MAIL command, since they require that you are authenticated before you send mail. You'll get a 530 response instead. You'll need to use the AUTH command with your gmail.com credentials to authenticate before you can use MAIL successfully on those servers.
If you don't want to authenticate, and depending on the details of what you need to do, you could try using port 25 of the server found in gmail.com's MX record. At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS.
After STARTTLS, call
clientSocket = ssl.wrap_socket(clientSocket)

xmpp with python: xmpp.protocol.InvalidFrom: (u'invalid-from', '')

cl = xmpp.Client('myserver.com')
if not cl.connect(server=('mysefver.com',5223)):
raise IOError('cannot connect to server')
cl.RegisterHandler('message',messageHandler)
cl.auth('myemail#myserver.com', 'mypassword', 'statusbot')
cl.sendInitPresence()
msgtext = formatToDo(cal, 'text')
message = xmpp.Message('anotheremail#myserver.com', msgtext)
message.setAttr('type', 'chat')
cl.send(message)
I get the following error message when I try to run it:
xmpp.protocol.InvalidFrom: (u'invalid-from', '')
Why is this happening :(
From the XMPP protocol specification:
If the value of the 'from'
address does not match the hostname represented by the Receiving
Server when opening the TCP connection (or any validated domain
thereof, such as a validated subdomain of the Receiving Server's
hostname or another validated domain hosted by the Receiving Server),
then the Authoritative Server MUST generate an stream
error condition and terminate both the XML stream and the underlying
TCP connection.
which basically means, that if the sender is not recognized by the xmpp-server, it'll reply with this message. XMPP supplies a registration mechanism: xmpp.features.register

Categories