Cannot communicate to SMTP server with socket and proxy due to timeout error

Here's my code for simple proxy validation:
import smtplib, socks
socks.set_default_proxy(socks.PROXY_TYPE_SOCKS5,
'*.*.*.*',
<PORT>,
username='<UNAME>',
password='<PWD>')
socks.wrapmodule(smtplib)
server = smtplib.SMTP('gmail-smtp-in.l.google.com', 25)
server.set_debuglevel(1) # SMTP dialogue log
server.ehlo()
server.quit()
Here's the output:
Traceback (most recent call last):
File "/Users/***/***/***/test.py", line 10, in <module>
server = smtplib.SMTP('gmail-smtp-in.l.google.com', 25)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 343, in connect
(code, msg) = self.getreply()
^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 405, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
I've pinged gmail-smtp-in.l.google.com
– it's fine.
I've also tried to check if we can establish connection with server through this proxy, and it seems like we can:
def test_proxy_connectivity(proxy, port):
ip, port_, user, pwd = proxy
try:
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, ip, port_, username=user, password=pwd)
s.settimeout(5)
s.connect(('gmail-smtp-in.l.google.com', port))
s.close()
print(f'proxy availible {ip}:{port_}')
return True
except Exception as e:
print(f'proxy UNavailible: {ip}:{port_} - {e}')
return False
test_proxy_connectivity(PROXY.values(),25)
Output:
proxy availible <SAMEPROXYHERE>
So it kinda makes me believe that port 25 is open on this proxy. And if we add recv there:
def test_proxy_connectivity(proxy, port):
ip, port_, user, pwd = proxy
try:
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, ip, port_, username=user, password=pwd)
s.settimeout(5)
s.connect(('gmail-smtp-in.l.google.com', port))
s.recv(1024).decode(errors='ignore')
s.close()
print(f'proxy availible {ip}:{port_}')
return True
except Exception as e:
print(f'proxy UNavailible: {ip}:{port_} - {e}')
return False
test_proxy_connectivity(PROXY.values(),25)
Output:
proxy UNavailible: <SAMEPROXYHERE> - timed out
The same problem I guess... And as far as I know this IP isn't in any blacklist like Spamhaus.
Answer
TCP error:
Connection unexpectedly closed
usually indicates incorrect SMTP settings, network connectivity issues, firewall rules, or the server's security policies rejecting the connection.
Did you try manually connecting as part of the troubleshooting? eg:
telnet some.smtp.server {25,587,465}
Choose one port (25,587,465) or try all 3 to see what's available. They each have different ways to authenticate. Port 25 is generally the unauthenticated port, port 587 will require authentication, and port 465 may work with some combination of both but generally requires authentication and/or might require you send TLS certificate after STARTTLS is issued.
Pinging is not enough to check if an SMTP server is responding. I do know for fact google SMTP servers will respond to all 3 as I have used some form of them in the past.
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions