在 Python 的 smtplib 包中,有没有办法在发送的电子邮件退回时创建通知?

In Python's smtplib package, is there a way to create a notification if sent emails bounce?

目前,我正在使用Python的smtplib包来发送电子邮件。我有一大堆电子邮件,我想验证它们是否存在。在大多数情况下,获得 250 条消息就足够了。但是,对于 aol.com,您可以放置​​任何句柄,他们仍然会说它存在。

所以,我在一一发帖。有没有办法编写一个例程,如果电子邮件没有通过,smtplib 会通知我电子邮件无效?谢谢!

如果无法发送电子邮件,则会引发异常as per the docs。您可以执行以下操作:

cantSendErrors = (smptlib.SMTPHeloError, smptlib.SMTPSenderRefused)  # Put any other suitable exceptions in this tuple
try:
    smptlib.sendmail(args, go, here)
except cantSendErrors:
    # Your code here. You could email yourself or write the error to log file for later review, your choice.