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 smtplib.py 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

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..

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.

This article has been dead for over six months. Start a new discussion instead.