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

Discussion in 'Python' started by Alex Hunsley, Jun 29, 2004.

  1. Alex Hunsley

    Alex Hunsley Guest

    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 = ''
    you = ''
    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
    Alex Hunsley, Jun 29, 2004
    #1
    1. Advertising

  2. Alex Hunsley

    Peter Hansen Guest

    Re: sending of mail (smtp) - connection refused - but smtp serveris

    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
    Peter Hansen, Jun 29, 2004
    #2
    1. Advertising

  3. Alex Hunsley

    Alex Hunsley Guest

    Re: sending of mail (smtp) - connection refused - but smtp serveris

    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
    Alex Hunsley, Jun 29, 2004
    #3
  4. Alex Hunsley

    Peter Hansen Guest

    Re: sending of mail (smtp) - connection refused - but smtp server

    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
    Peter Hansen, Jun 29, 2004
    #4
  5. Re: sending of mail (smtp) - connection refused - but smtp serveris running!

    On Tue, 29 Jun 2004 15:26:41 +0100, Alex Hunsley
    <> 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 = ''
    > you = ''
    > 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()
    Tim Williams (gmail), Jun 29, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. matamoscas

    connection refused using smtp

    matamoscas, Sep 2, 2005, in forum: Java
    Replies:
    4
    Views:
    6,260
    matamoscas
    Sep 7, 2005
  2. Replies:
    3
    Views:
    1,066
    Roedy Green
    Jan 24, 2006
  3. Raaijmakers, Vincent \(GE Infrastructure\)

    RE: sending of mail (smtp) - connection refused - but smtp serveris running!

    Raaijmakers, Vincent \(GE Infrastructure\), Jun 29, 2004, in forum: Python
    Replies:
    0
    Views:
    877
    Raaijmakers, Vincent \(GE Infrastructure\)
    Jun 29, 2004
  4. Replies:
    0
    Views:
    1,356
  5. howa
    Replies:
    6
    Views:
    421
    Thomas Fritsch
    Feb 21, 2007
Loading...

Share This Page