[Tutor] Smtplib and exceptions- why it does NOT work

A printers@sendme.cz
Mon, 20 May 2002 22:40:14 +0200


Hi,
How can I get response from Smtplib?
For example I have 

class SMTPM:
    def sendMail(self):
        import smtplib
        s11 = smtplib.SMTP('smtp.sendme.cz')
        Response=s11.sendmail('a@NonExistingDomain1.com',"printers@sendme.cz","TEST")
        print Response
A=SMTPM()
A.sendMail()

When I start the class I will get the following exception:

SMTPSenderRefused: (501, '5.1.8 <a@NonExistingDomain1.com>... Domain of sender address a@NonExistingDomain1.com does not exist', 'a@NonExistingDomain1.com')
and execution stops.
I,however, would like to print only
(501, '5.1.8 <a@NonExistingDomain1.com>... Domain of sender address a@NonExistingDomain1.com does not exist', 'a@NonExistingDomain1.com')
without stopping execution. 

So I handled that exception and changed the class like this

class SMTPM:
    
    def sendMail(self):
        import smtplib
        self.s11 = smtplib.SMTP('smtp.sendme.cz')
        
        try: 
            Response=self.s11.sendmail('a@NonExistingDomain.com',"printers@sendme.cz","TEST")
        except(smtplib.SMTPRecipientsRefused,smtplib.SMTPSenderRefused ):
            print "Response ",Response
A=SMTPM()
A.sendMail()

But it does not work.
Can anybody please explain to me why?
thanks
Ladislav