[英]Python mail sending error: "CERTIFICATE VERIFY FAILED" & "WRONG VERSION NUMBER"
Through the smtp email server, I'm attempting to send an email. I tried using the SMTP SSL() and starttls() methods to send emails, but both returned errors: WRONG VERSION NUMBER for SMTP SSL() and CERTIFICATE VERIFY FAILED for starttls().通过 smtp email 服务器,我试图发送 email。我尝试使用 SMTP SSL() 和 starttls() 方法发送电子邮件,但都返回错误:WRONG VERSION NUMBER for SMTP SSL() and CERTIFY FCAILE VER for开始()。 Below are the given tracebacks.
以下是给定的回溯。
Traceback of SMTP_SSL(): SMTP_SSL() 的回溯:
File "mail_sender_test.py", line 15, in <module>
with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
File "/usr/lib64/python3.6/smtplib.py", line 1031, in __init__
source_address)
File "/usr/lib64/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python3.6/smtplib.py", line 1039, in _get_socket
server_hostname=self._host)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:897)
Traceback of starttls(): starttls() 的回溯:
File "mail_sender_test.py", line 21, in <module>
server.starttls(context=context)
File "/usr/lib64/python3.6/smtplib.py", line 771, in starttls
server_hostname=self._host)
File "/usr/lib64/python3.6/ssl.py", line 365, in wrap_socket
_context=self, _session=session)
File "/usr/lib64/python3.6/ssl.py", line 776, in __init__
self.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 1036, in do_handshake
self._sslobj.do_handshake()
File "/usr/lib64/python3.6/ssl.py", line 648, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:897)
Here is the sample code.这是示例代码。
import smtplib, ssl
smtp_server = "1.2.3.4"
smtp_port = 25
smtp_password = "1234"
sender_address = "abcd@def.com"
receiver_address = "xyz@gmail.com"
message = """\
Subject: Hi there
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
server.login(sender_address, smtp_password)
server.sendmail(sender_address, receiver_address, message)
# with smtplib.SMTP(smtp_server, smtp_port) as server:
# server.ehlo() # Can be omitted
# server.starttls(context=context)
# server.ehlo() # Can be omitted
# server.login(sender_address, smtp_password)
# server.sendmail(sender_address, receiver_address, message)
If anyone could assist in solving the problem, that would be wonderful.如果有人可以协助解决问题,那就太好了。
Thanks谢谢
smtp_port = 25... with smtplib.SMTP_SSL(smtp_server, smtp_port, context=context) as server:
Port 25 is for plain SMTP with a possible upgrade to TLS with starttls
.端口 25 用于普通的 SMTP,可以使用
starttls
升级到 TLS。 SMTP_SSL
instead is for SMTP directly over TLS. SMTP_SSL
而是直接通过 TLS 用于 SMTP。 Thus your code tries to speak TLS with a non-TLS peer.因此,您的代码会尝试与非 TLS 对等方通信 TLS。 The peer sends something npn-TLS (likely something like
220...
) which gets wrongly interpreted as TLS though, leading to WRONG_VERSION_NUMBER.对等方发送一些 npn-TLS(可能类似于
220...
),但它被错误地解释为 TLS,导致 WRONG_VERSION_NUMBER。
... CERTIFICATE VERIFY FAILED for starttls()
... starttls() 的证书验证失败
That's the case if the servers certificate cannot be verified.如果无法验证服务器证书,就会出现这种情况。 Reasons are unknown since nothing is known about your server, its certificate and your trust store.
原因未知,因为对您的服务器、它的证书和您的信任库一无所知。 Note that if you have a self-signed certificate you need to explicitly trust it by setting the SSL context appropriately.
请注意,如果您有自签名证书,则需要通过适当地设置 SSL 上下文来显式信任它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.