423,319 Members | 2,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 = 'a*******@companyname.com'
you = 's*********@companyname.com'
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Alex Hunsley wrote:
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:
from socket import *
getaddrinfo('192.168.1.105', 25, 0, SOCK_STREAM)

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
Jul 18 '05 #2

P: n/a
Peter Hansen wrote:
Alex Hunsley wrote:
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:
>>> from socket import *
>>> getaddrinfo('192.168.1.105', 25, 0, SOCK_STREAM)

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

Jul 18 '05 #3

P: n/a
Alex Hunsley wrote:
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
Jul 18 '05 #4

P: n/a
On Tue, 29 Jun 2004 15:26:41 +0100, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:

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 = 'a*******@companyname.com'
you = 's*********@companyname.com'
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()

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.