我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用smtplib.SMTPResponseException()。
def email(handle, matches): """Emails a list of people""" company_output = ', '.join([comp.upper() for comp in matches]) try: with open('./Files/emails.txt') as f: email_list = f.read().split() server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(EMAIL, PASSWORD) server.sendmail(EMAIL, email_list, f'{handle} just tweeted about {company_output}. ' f'Might be time to check your shares...') server.quit() except smtplib.SMTPResponseException as error: logging.debug(error)
def testLineTooLong(self): self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP, HOST, self.port, 'localhost', 3)
def test_with_statement_QUIT_failure(self): with self.assertRaises(smtplib.SMTPResponseException) as error: with smtplib.SMTP(HOST, self.port) as smtp: smtp.noop() self.serv._SMTPchannel.quit_response = '421 QUIT FAILED' self.assertEqual(error.exception.smtp_code, 421) self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') #TODO: add tests for correct AUTH method fallback now that the #test infrastructure can support it. # Issue 5713: make sure close, not rset, is called if we get a 421 error
def test_with_statement_QUIT_failure(self): with self.assertRaises(smtplib.SMTPResponseException) as error: with smtplib.SMTP(HOST, self.port) as smtp: smtp.noop() self.serv._SMTPchannel.quit_response = '421 QUIT FAILED' self.assertEqual(error.exception.smtp_code, 421) self.assertEqual(error.exception.smtp_error, b'QUIT FAILED') #TODO: add tests for correct AUTH method fallback now that the #test infrastructure can support it. # Issue 17498: make sure _rset does not raise SMTPServerDisconnected exception
def contact(self): subject = self.cleaned_data['subject'] from_email = self.cleaned_data['email'] message = self.cleaned_data['body'] try: return send_mail(subject, message, from_email, settings.ADMINS) except SMTPResponseException: return False
def send_messages(self, email_messages): """ Override the from_email property all email messages. """ if not email_messages: return with self._lock: for message in email_messages: message.from_email = get_site_setting('smtp_from_address') try: super(EmailBackend, self).send_messages(email_messages) except (SMTPResponseException, socket_error) as e: # TODO: Determine how to handle failures gracefully. raise e
def sendmail(self, from_addr: str, to_addrs: Sequence[str], msg: bytes, mail_options: List[str]=[], rcpt_options: List[str]=[]) -> Union[str, None]: """ Wraps smtplib.sendmail and handles all the exceptions it can throw. :return: a SMTP return string or None """ with smtplib.SMTP(self.external_ip, self.external_port) as smtp: try: smtp.sendmail(from_addr, to_addrs, msg, mail_options, rcpt_options) except smtplib.SMTPSenderRefused as e: if isinstance(e.smtp_error, bytes): errorstr = e.smtp_error.decode("utf-8", errors="ignore") else: errorstr = str(e.smtp_error) _log.info("Downstream server refused sender: %s (%s %s)", e.sender, e.smtp_code, errorstr) return "%s %s" % (e.smtp_code, e.smtp_error) except smtplib.SMTPResponseException as e: # This exception baseclass is for all exceptions that have a SMTP response code. # Return the downstream error code upstream if isinstance(e.smtp_error, bytes): errorstr = e.smtp_error.decode("utf-8", errors="ignore") else: errorstr = str(e.smtp_error) _log.info("Unexpected response from server (passed upstream): %s %s", e.smtp_code, errorstr) return "%s %s" % (e.smtp_code, errorstr) except smtplib.SMTPRecipientsRefused as e: _log.info("Some recipients where refused by the downstream server: %s", ", ".join(e.recipients.keys())) if self.internal_ip and self.internal_port: with smtplib.SMTP(self.internal_ip, self.internal_port) as smtp_r: try: smtp_r.sendmail( "<>", [from_addr], self._format_denied_recipients(msg, list(e.recipients.keys())) ) except smtplib.SMTPException as ex: _log.exception("Error while sending denied recipients reply: %s", str(ex)) return None except smtplib.SMTPServerDisconnected as e: _log.info("Downstream server unexpectedly disconnected: %s", str(e)) return "421 Possible network problem. Please try again." return None # patch the SMTP channel implementation to pass us a reference to the channel # and use sane logging