Hi
I'm having a test suite in python and i would like to email the result through my company server. I used a similar code but the code seems to be hung while waiting for a response from server during socket.connect() call. I traced the code and found that it enters the connect() method in and from there it enters readline() method in socket.py......this is where is waits forever for response from mail server.
If I do a "netstat -a" on DOS prompt, I can see that connection is ESTABLISHED between mail server and the connection I initiated via python script.

FYI: if I do a ping MYSERVER, I can see the response, So I am sure, the mail server on our network is responding!

import os
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64

def sendMail(subject, text):
    UserName = 'test.user@mailserver.com'
    UserPwd = 'mypassword'
    recipient = 'test.user@mailserver.com'

    msg = MIMEMultipart()
    msg['From'] = UserName
    msg['To'] = recipient
    msg['Subject'] = subject
    msg.attach(MIMEText(text))   
    mailServer = smtplib.SMTP('192.168.178.22',1352)   
    mailServer.login(UserName, UserPwd)
    mailServer.set_debuglevel(1)
    mailServer.sendmail(UserName, recipient, msg.as_string())
    mailServer.quit()

    print('Sent email to %s' %(recipient))
   
   
if __name__ == '__main__':
    sendMail('hi','hello how are u')

Any comments would be of great help....thank you

Dani AI

Generated

Agreeing with : the hang you see after connect() is classic “connected to the wrong service.” TCP 1352 is Domino’s NRPC port, not SMTP. NRPC will happily accept the TCP handshake but never send the SMTP 220 greeting that smtplib waits for, so your client blocks. Ping/netstat only prove a socket is open, not that SMTP is speaking on that port. (help.hcl-software.com)

Fix: point smtplib at your SMTP listener. Use 25 for server-to-server SMTP, or 587 for authenticated submission with STARTTLS; 465 is for implicit TLS if your server requires it. Turn on debug before connecting and add a timeout so the script cannot hang. On Python 2.5 specifically, SMTP(..., timeout=...) was not added until 2.6, so set a global socket timeout first. (iana.org)

# Minimal handshake you can drop into your test
import socket, smtplib
socket.setdefaulttimeout(60)           # Py 2.5: avoid indefinite waits
s = smtplib.SMTP()                     # create first so debug captures connect
s.set_debuglevel(1)
s.connect("192.168.178.22", 25)        # try 25 (or 587 if your org uses submission)
s.ehlo()
# if using 587 and server advertises STARTTLS, do:
# s.starttls(); s.ehlo()
# s.login("user", "password"); s.sendmail(from_addr, to_addr, msg)
s.quit()

Two quick checks before rerunning your suite:

  • Telnet or netcat to the target port and look for a line starting with “220 …” right after connect; if you do not see it, you are not on SMTP. (rfc-editor.org)
  • If you test with Gmail (per ’s idea), use smtp.gmail.com:587 with STARTTLS and OAuth2 or an App Password; simple username/password is blocked in many cases today, especially for Google Workspace. (developers.google.com)

If your company runs Domino, ensure the SMTP task is enabled and listening on 25/587, not just NRPC on 1352. (help.hcl-software.com)

Recommended Answers

All 8 Replies

have you tried with Gmail settings instead of your server? That should give you idea if the problem is the script or your server

Yes i did try out the gmail settings but the error persists.
The problem is that when i run the command "netstat -a" it shows me that the connection is successfully established.
plus the ping tells me that server is running fine.
now the script too looks fine which puts me in dilemma..

which version of python do you use? I have to test it for myself later

python 2.5

Thanks alot.... :->

I will check when at home and feedback (which may be some hours)
But worry not daniweb have many developers :lol:

I tried out your code, and it worked fine for me. There were a couple changes that I made that were specific for my environment. For example, my mail server does not support authentication, so I commented out line 22 (mailserver.login).

The most significant change that I made, and one that you should definitely look at is on line 21 where you set the mail server. Right now you're connecting to port 1352, but most smtp servers run on port 25. It is possible that your server is running on 1352, but I usually don't see smtp running on a non-standard port. If there is anything listening on 1352 then your script would still connect, but if the mail server isn't responding on port 1352 then you would see a connection, and then the script would hang because it was expecting some response from the server. I would double check that port that you're using and see if it shouldn't be 25.

i will definitely verify this.
thank you so much..

thanks a lot its a easy smtplib example thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.