本文主要是介绍python 调用smtplib 发送邮件问题(Python 增加starttls(), Perl中增加doSSL 参数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近公司切换网络采用新的邮件服务器后,Python不能调用新的服务器发送邮件,报以下错误。
(PS 邮件服务器的地址要管理员在office365上用MX 方式生成新的服务器地址供python调用)
报错一:
smtplib.SMTPRecipientsRefused:
{'xx.xx@arraycomm.com': (550, b'5.7.606 Access denied, banned sending IP [xx.150.46.xx]. To request removal from this list please visit https://sender.office.com/ and follow the directions. For more information please go to http://go.microsoft.com/fwlink/?LinkID=526655 AS(1430) [SHAFFO30FD005.protectioncn.gbl]'),
'xx.xx@arraycomm.com': (550, b'5.7.606 Access denied, banned sending IP [xx.150.46.xx]. To request removal from this list please visit https://sender.office.com/ and follow the directions. For more information please go to http://go.microsoft.com/fwlink/?LinkID=526655 AS(1430) [SHAFFO30FD005.protectioncn.gbl]')}
报错二:
File "C:\Python36\lib\smtplib.py", line 387, in getreply
line = self.file.readline(_MAXLINE + 1)
File "C:\Python36\lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
在python代码中原来的code:
self.smtpObj.sendmail(self.sender, toUsers, message.as_string())
改成以下,可以收到邮件:
self.smtpObj.ehlo()
self.smtpObj.starttls()
self.smtpObj.sendmail(self.sender, toUsers, message.as_string())
再改成也可以收到邮件:
self.smtpObj.starttls()
self.smtpObj.sendmail(self.sender, toUsers, message.as_string())
查看了下加的函数:
def starttls(self, keyfile=None, certfile=None, context=None):
"""Puts the connection to the SMTP server into TLS mode.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first.
If the server supports TLS, this will encrypt the rest of the SMTP
session. If you provide the keyfile and certfile parameters,
the identity of the SMTP server and client can be checked. This,
however, depends on whether the socket module really checks the
certificates.
This method may raise the following exceptions:
SMTPHeloError The server didn't reply properly to
the helo greeting.
同时如果Perl中也出现这个问题,解决方法:
改用Net::SMTPS 模块,在new方法中增加doSSL 参数。亲测有效。
这篇关于python 调用smtplib 发送邮件问题(Python 增加starttls(), Perl中增加doSSL 参数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!