在使用yagmail库批量发送邮件时,如果遇到某个邮箱地址不存在,你可以通过异常处理来保证其他邮箱地址能够成功发送。
import yagmail
yag = yagmail.SMTP(user='xxxxxx@xxxx.com',
password='xxxxxx', host='smtp.qiye.aliyun.com', port=465)
subject = ['ceshi']
to = ['已离职@xxx.com','xxxx@xxxxx.com']
subject = 'test'
contents1 = 'agag'
# 发送邮件
try:
yag.send(to, subject, contents=contents1)
except yagmail.SMTPError as e:
print(f"Failed to send email to {email_address}: {str(e)}")
print('邮件已发送')
yag.close()
在上述示例中,如果遇到`SMTPRecipientsRefused`异常(表示收件人地址无效),它将被捕获并记录到日志或打印出来。然后程序会继续执行下一个循环,发送给其他有效的邮箱地址。