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

Peter Hansen peter at engcorp.com
Tue Jun 29 12:04:35 EDT 2004


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



More information about the Python-list mailing list