Python 3.x 使用SMTPLib发送电子邮件时出现SSL错误

Python 3.x 使用SMTPLib发送电子邮件时出现SSL错误,python-3.x,ssl,smtplib,Python 3.x,Ssl,Smtplib,我正在编写一个小的Python程序,其中包括向游戏玩家发送电子邮件。我目前正在使用SMTPlib来实现这一点,这是第一次,虽然它工作得很好,但现在每当我尝试运行它时,都会出现以下错误: Traceback (most recent call last): File "/Users/user/Desktop/test.py", line 17, in <module> sendEmail() File "/Users/user/Deskto

我正在编写一个小的Python程序,其中包括向游戏玩家发送电子邮件。我目前正在使用SMTPlib来实现这一点,这是第一次,虽然它工作得很好,但现在每当我尝试运行它时,都会出现以下错误:

Traceback (most recent call last):
  File "/Users/user/Desktop/test.py", line 17, in <module>
    sendEmail()
  File "/Users/user/Desktop/test.py", line 11, in sendEmail
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1034, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1041, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1122)
我怀疑这个错误与我使用Python 3.9.0而不是3.7.6有关,因为这是我的环境自上次运行以来唯一发生变化的地方,但我对此不确定*。导致此错误的原因是什么?我可以做些什么来修复它

*编辑:我在Python3.7.6上运行了相同的代码,以防出现问题。有趣的是,它现在给出了一个非常类似的错误:

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    sendEmail()
  File "test.py", line 11, in sendEmail
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1039, in _get_socket
    server_hostname=self._host)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
回溯(最近一次呼叫最后一次):
文件“test.py”,第17行,在
发送电子邮件()
sendEmail中第11行的文件“test.py”
使用smtplib.SMTP_SSL(“SMTP.gmail.com”,端口,上下文=上下文)作为服务器:
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py”,第1031行,在__
来源(地址)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py”,第251行,在__
(代码,消息)=自连接(主机,端口)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py”,第336行,在connect中
self.sock=self.\u获取\u套接字(主机、端口、self.timeout)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py”,第1039行,在
服务器(主机名=self.\u主机)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第423行,在wrap_套接字中
会话=会话
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第870行,在创建
self.do_握手
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”,第1139行,在do_握手中
赛尔夫:握手
ssl.SSLCertVerificationError:[ssl:证书\u验证\u失败]证书验证失败:无法获取本地颁发者证书(\u ssl.c:1076)

如果这是您怀疑的,您应该尝试使用Python 3.7.6运行您的代码,并告诉我们这是否是原因;)编辑了我的文章,将其作为一种尝试性的解决方案。你看过这个吗?是的,我尝试了top answer的解决方案,以及
pip install certifi
,但它也不起作用:/
Traceback (most recent call last):
  File "test.py", line 17, in <module>
    sendEmail()
  File "test.py", line 11, in sendEmail
    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1031, in __init__
    source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 1039, in _get_socket
    server_hostname=self._host)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)