sending of mail (smtp) - connection refused - but smtp server isrunning!

A

Alex Hunsley

I am using the smtp module to send emails via a local SMTP server on our network.
I am failing with "connection refused" error, even though we definitely have an
smtp server running on port 25!

the code is like this:


me = '(e-mail address removed)'
you = '(e-mail address removed)'
msg['Subject'] = '*** alert'
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('192.168.1.105')
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

print "Email sent."

When I run this code, I get:


Traceback (most recent call last):
File "C:\Python23\check.py", line 58, in -toplevel-
s.connect()
File "C:\Python23\lib\smtplib.py", line 302, in connect
raise socket.error, msg
error: (10061, 'Connection refused')


here's me verifying that SMTP server is indeed alive at port 25 on my local
network:

$ telnet 192.168.1.105 25
Trying 192.168.1.105...
Connected to 192.168.1.105.
Escape character is '^]'.
220 grain.companyname.com ESMTP Server FTGate
helo
250 grain.companyname.com


any ideas what the problem could be? this usually happens when someone is not
aware they have to run an SMTP server, but we do have one running, as can be
seen above!

thanks
alex
 
P

Peter Hansen

Alex said:
I am failing with "connection refused" error, even though we definitely
have an smtp server running on port 25! ....
any ideas what the problem could be? this usually happens when someone
is not aware they have to run an SMTP server, but we do have one
running, as can be seen above!

Not sure, but please try this at the interactive prompt and report
(or analyze) what it returns:


This is something that smtplib.py is actually doing to get the
host:port combination that it uses to connect. It seems that
the only possible source for the failure you see is if it
does not return the expected values.

-Peter
 
A

Alex Hunsley

Peter said:
Not sure, but please try this at the interactive prompt and report
(or analyze) what it returns:



This is something that smtplib.py is actually doing to get the
host:port combination that it uses to connect. It seems that
the only possible source for the failure you see is if it
does not return the expected values.

-Peter

Hi Peter
when I run the above interactively, I get:

[(2, 1, 6, '', ('192.168.1.105', 25))]

I've just discovered something interesting.
If I completely miss out connect() and close(), it works!
i.e. my code now reads:

s = smtplib.SMTP('192.168.1.105')
#s.connect()
s.sendmail(me, [you], msg.as_string())
#s.close()


and this works! Is it a bad state of affairs to leave things in?
thanks
alex
 
P

Peter Hansen

Alex said:
I've just discovered something interesting.
If I completely miss out connect() and close(), it works!
i.e. my code now reads:

s = smtplib.SMTP('192.168.1.105')
#s.connect()
s.sendmail(me, [you], msg.as_string())
#s.close()


and this works! Is it a bad state of affairs to leave things in?

Oh, silly me! I should have seen this the first time.
This is exactly what you should expect, since the call
to the SMTP() constructor calls its self.connect() method
automatically if you specify the host argument!

The docs for smtplib say these things near the top:

'''If the optional host and port parameters are given, the
SMTP connect() method is called with those parameters during
initialization. '''

'''For normal use, you should only require the
initialization/connect, sendmail(), and quit() methods.
An example is included below.'''

To clean things up, you should be calling .quit() instead
of .close(), although in actual fact they basically do
the same thing. (Check the source in smtplib.py to learn
more.)

-Peter
 
T

Tim Williams (gmail)

I am using the smtp module to send emails via a local SMTP server on our network.
I am failing with "connection refused" error, even though we definitely have an
smtp server running on port 25!

the code is like this:

me = '(e-mail address removed)'
you = '(e-mail address removed)'
msg['Subject'] = '*** alert'
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('192.168.1.105')
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

print "Email sent."

When I run this code, I get:

Traceback (most recent call last):
File "C:\Python23\check.py", line 58, in -toplevel-
s.connect()
File "C:\Python23\lib\smtplib.py", line 302, in connect
raise socket.error, msg
error: (10061, 'Connection refused')

Try this

s = smtplib.SMTP('192.168.1.105')
failed = s.sendmail(me, [you], msg.as_string())
s.quit()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top