使用SMTPLib发送电子邮件时出现SSL错误

时间:2020-10-30 13:35:06

标签: 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/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)

以及引发此错误的准系统代码:

import smtplib, ssl

def sendEmail():
    port = 465  # For SSL
    password = 'password'
    senderEmail = 'senderEmail@email.com'
    
    # Create a secure SSL context
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
        server.login(senderEmail, password)
        message = 'Hello world!'
        
        server.sendmail(senderEmail, 'myEmail@email.com', message)

sendEmail()

我怀疑这个错误与我使用Python 3.9.0而不是3.7.6有关,因为自上次运行以来,这是唯一改变我的环境的事情,但我不确定那*。是什么导致此错误,我该怎么解决?

* EDIT:如果是问题的话,我在Python 3.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)

0 个答案:

没有答案