In our previous article we have discussed about properties and definition of SMTPSenderRefused exception in SMTP module. If you have not gone through that , please take a look by clicking this link Exceptions - SMTPSenderRefused(); . We have discussed this exception of SMTP library throughly for clarity and good understanding on the topic.
Now in this article we will discuss about property, definition and usage of SMTPRecipientsRefused exception. This article will specifically give you a brief idea about SMTPRecipientsRefused exception in SMTP library.
(v) exception smtplib.SMTPRecipientsRefused
The cause for this SMTPRecipientsRefused exception is that the SMTP server is refusing some of the recipients or all of the recipients, you're sending email to. The fix is either to not send email to those recipients, to reconfigure the SMTP server to accept them, or to find a different SMTP server to use. Your mail server isn't allowed to send mail. The errors for each recipient are accessible through the attribute recipients, which is a dictionary of exactly the same sort as SMTP.sendmail() returns.
Constructors and Destructor
def __init__(self, recipients):
self.recipients = recipients
self.args = (recipients,)
The following code is example for showing how to use smtp.SMTPRecipientsrefused exception . This code is from open source Python projects.
def _deliver(self, mailfrom, rcpttos, data):
import smtplib
refused = {}
try:
s = smtplib.SMTP()
s.connect(self._remoteaddr[0], self._remoteaddr[1])
try:
refused = s.sendmail(mailfrom, rcpttos, data)
finally:
s.quit()
except smtplib.SMTPRecipientsRefused as e:
print('got SMTPRecipientsRefused', file=DEBUGSTREAM)
refused = e.recipients
except (OSError, smtplib.SMTPException) as e:
print('got', e.__class__, file=DEBUGSTREAM)
# All recipients were refused. If the exception had an associated
# error code, use it. Otherwise,fake it with a non-triggering
# exception code.
errcode = getattr(e, 'smtp_code', -1)
errmsg = getattr(e, 'smtp_error', 'ignore')
for r in rcpttos:
refused[r] = (errcode, errmsg)
return refused
In this example, we learn that how to use smtplib.SMTPRecipientsRefused exception. When the program server(SMTP server) refuses to take some or all of the recipients, this exception occurs. If the exception has an error code associated with it we can use that also.
Note:- In the next article we will discuss on smtp.SMTPDataError exception . Keep on checking.
Comments