gpt4 book ai didi

python - SMTP_SSL SSLError : [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl. c:590)

转载 作者:行者123 更新时间:2023-11-28 17:15:07 24 4
gpt4 key购买 nike

此问题与 smtplib 的 SMTP_SSL 连接有关。

当与 SMTP 连接时(没有 ssl),它正在工作。

在 SMTP_SSL 中尝试相同的主机和端口时,出现错误。该错误仅基于主机,gmail 设置也可以正常工作。

请检查以下示例,如果 outlook 和 office365 需要任何更改,请告诉我。

In [1]: import smtplib

In [10]: smtplib.SMTP_SSL('smtp.gmail.com', 465, timeout=100)
Out[10]: <smtplib.SMTP_SSL instance at 0x109ccaf38>

In [2]: smtplib.SMTP('smtp-mail.outlook.com', 587, timeout=100)
Out[1]: <smtplib.SMTP instance at 0x10a00bb90>

In [3]: smtplib.SMTP_SSL('smtp-mail.outlook.com', 587, timeout=100)
---------------------------------------------------------------------------
SSLError Traceback (most recent call last)
<ipython-input-9-f0c5b0de4e24> in <module>()
----> 1 smtplib.SMTP_SSL('smtp-mail.outlook.com', 587, timeout=100)

/python2.7/smtplib.pyc in __init__(self, host, port, local_hostname, keyfile, certfile, timeout)
794 self.keyfile = keyfile
795 self.certfile = certfile
--> 796 SMTP.__init__(self, host, port, local_hostname, timeout)
797
798 def _get_socket(self, host, port, timeout):

python2.7/smtplib.pyc in __init__(self, host, port, local_hostname, timeout)
254 self.esmtp_features = {}
255 if host:
--> 256 (code, msg) = self.connect(host, port)
257 if code != 220:
258 raise SMTPConnectError(code, msg)

python2.7/smtplib.pyc in connect(self, host, port)
314 if self.debuglevel > 0:
315 print>>stderr, 'connect:', (host, port)
--> 316 self.sock = self._get_socket(host, port, self.timeout)
317 (code, msg) = self.getreply()
318 if self.debuglevel > 0:

python2.7/smtplib.pyc in _get_socket(self, host, port, timeout)
800 print>>stderr, 'connect:', (host, port)
801 new_socket = socket.create_connection((host, port), timeout)
--> 802 new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)
803 self.file = SSLFakeFile(new_socket)
804 return new_socket

python2.7/ssl.pyc in wrap_socket(sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, suppress_ragged_eofs, ciphers)
909 do_handshake_on_connect=do_handshake_on_connect,
910 suppress_ragged_eofs=suppress_ragged_eofs,
--> 911 ciphers=ciphers)
912
913 # some utility functions

python2.7/ssl.pyc in __init__(self, sock, keyfile, certfile, server_side, cert_reqs, ssl_version, ca_certs, do_handshake_on_connect, family, type, proto, fileno, suppress_ragged_eofs, npn_protocols, ciphers, server_hostname, _context)
577 # non-blocking
578 raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
--> 579 self.do_handshake()
580
581 except (OSError, ValueError):

python2.7/ssl.pyc in do_handshake(self, block)
806 if timeout == 0.0 and block:
807 self.settimeout(None)
--> 808 self._sslobj.do_handshake()
809 finally:
810 self.settimeout(timeout)

SSLError: [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:590)

引用网址:https://docs.python.org/2/library/smtplib.html#smtplib.SMTP_SSL

最佳答案

我今天遇到了同样的问题,我已经解决了。

这是我的演示。

def _send_email(self, msg):
if self.smtp_port == 465:
server = smtplib.SMTP_SSL(self.smtp_host, self.smtp_port)
elif self.smtp_port == 587:
server = smtplib.SMTP(self.smtp_host, self.smtp_port)
server.starttls()
else:
raise Exception('port {} not support'.format(SMTP_PORT))
server.set_debuglevel(1)
server.login(self.from_addr, self.password)
server.sendmail(self.from_addr, [self.to_addr], msg.as_string())
server.quit()

个人看法(可能有误,个人理解,如有不妥,请大家指正,谢谢。):

465587 用于不同的协议(protocol)。前者使用SSL,后者使用TLS。它们都是在SMTP通信之前使用SSL加密我们的邮件内容。

但对于 587。您必须先发送 STARTTLS。如果你不这样做,你会收到这样的消息 Must issue a STARTTLS command first. 而对于 465 你不能使用 tls 命令

关于python - SMTP_SSL SSLError : [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl. c:590),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com