diff -r 2873f4971281 Lib/smtplib.py --- a/Lib/smtplib.py Wed Mar 20 18:16:05 2013 +0200 +++ b/Lib/smtplib.py Thu Mar 21 00:18:00 2013 +0530 @@ -751,7 +751,10 @@ esmtp_opts.append(option) (code, resp) = self.mail(from_addr, esmtp_opts) if code != 250: - self.rset() + if code == 421: + self.close() + else: + self.rset() raise SMTPSenderRefused(code, resp, from_addr) senderrs = {} if isinstance(to_addrs, str): @@ -760,13 +763,19 @@ (code, resp) = self.rcpt(each, rcpt_options) if (code != 250) and (code != 251): senderrs[each] = (code, resp) + if code == 421: + self.close() + raise SMTPRecipientsRefused(senderrs) if len(senderrs) == len(to_addrs): # the server refused all our recipients self.rset() raise SMTPRecipientsRefused(senderrs) (code, resp) = self.data(msg) if code != 250: - self.rset() + if code == 421: + self.close() + else: + self.rset() raise SMTPDataError(code, resp) #if we got here then somebody got our mail return senderrs diff -r 2873f4971281 Lib/test/test_smtplib.py --- a/Lib/test/test_smtplib.py Wed Mar 20 18:16:05 2013 +0200 +++ b/Lib/test/test_smtplib.py Thu Mar 21 00:18:00 2013 +0530 @@ -807,6 +807,9 @@ with self.assertRaises(smtplib.SMTPResponseException) as error: with smtplib.SMTP(HOST, self.port) as smtp: smtp.noop() + # Next two asserts make sure that smtp.close() was called + self.assertIsNone(smtp.sock) + self.assertIsNone(smtp.file) self.assertEqual(error.exception.smtp_code, 421) self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') # We don't need to clean up self.serv.quit_response because a new