Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一次连接smtp服务器并在长时间内发送多封电子邮件可以吗?_Python_Email_Smtp - Fatal编程技术网

Python 一次连接smtp服务器并在长时间内发送多封电子邮件可以吗?

Python 一次连接smtp服务器并在长时间内发送多封电子邮件可以吗?,python,email,smtp,Python,Email,Smtp,这是我大学的代码,但我认为这不是很有效,因为它每次都会连接smtp服务器并登录,只发送一封邮件…,那么,在服务启动后,我第一次连接smtp并登录,然后使用这个长连接将邮件发送到另一个地方如何 def send_email(receiver, subject, mail_body): msg = MIMEText(mail_body, _subtype='html', _charset='utf-8') msg['Subject'] = Header(subject, 'utf-8

这是我大学的代码,但我认为这不是很有效,因为它每次都会连接smtp服务器并登录,只发送一封邮件…,那么,在服务启动后,我第一次连接smtp并登录,然后使用这个长连接将邮件发送到另一个地方如何

def send_email(receiver, subject, mail_body):
    msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['From'] = XXXX@xx.com
    msg['To'] = receiver

    try:
        smtp = smtplib.SMTP()
        smtp.connect(xxxx.com)
        smtp.login(user, password)
        smtp.sendmail(XXXX@xx.com, receiver.split(','), msg.as_string())
    except Exception:
        logger.error('Send email failed: %s' % traceback.format_exc())
    finally:
        smtp.quit()
使用上面的代码,我测试使用相同的连接发送10封电子邮件,但下面出现异常,我认为这可能与目标smtp服务器的安全策略有关

[2021-03-01 15:56:25,386] [ERROR] [125:MainThread] [send_email.py:47] [send_email]:send email failed: Traceback (most recent call last):
File "send_email.py", line 41, in send_email
smtp.sendmail(EmailAccount, email_receiver.split(','), msg.as_string())
File "/usr/lib/python2.7/smtplib.py", line 737, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (450, 'Requested mail action not taken: too much delivery in this connection', 'xxxx@xxxx.com')

可以重用连接来发送电子邮件,但服务器可能随时终止连接,这取决于服务器的策略。因此,只需捕获相应的错误并重新连接。

调用
smtp.sendmail(…)
任意多个,并在完成所有发送作业后执行
smtp.quit()
。或者不要将
smtp.quit()
放在函数中,将其拆分到另一个函数并重写程序。如果我在一年内从未退出连接,会怎么样?服务器是免费的,可以随时终止连接,因此可能不会有连接闲置那么长时间。如果您想重用连接,您必须实现代码,以便在出现错误时确定它们是临时的(在本例中,实现重试,可能需要等待一段时间)还是永久的(例如错误的凭据);在这种情况下,中止并将错误代码和消息通知用户。也请看,谢谢,这正是我想要的,也许你可以把这些评论放在答案中。
[2021-03-01 15:56:25,386] [ERROR] [125:MainThread] [send_email.py:47] [send_email]:send email failed: Traceback (most recent call last):
File "send_email.py", line 41, in send_email
smtp.sendmail(EmailAccount, email_receiver.split(','), msg.as_string())
File "/usr/lib/python2.7/smtplib.py", line 737, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
SMTPSenderRefused: (450, 'Requested mail action not taken: too much delivery in this connection', 'xxxx@xxxx.com')