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
    mailServer = smtplib.SMTP('',1352)   
    mailServer.login(UserName, UserPwd)
    mailServer.sendmail(UserName, recipient, msg.as_string())

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

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

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, learning, and sharing knowledge.