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 参数。亲测有效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值