SSL证书使用Python发送电子邮件时验证故障错误

发布于 2025-01-20 23:56:10 字数 2928 浏览 3 评论 0 原文

我正在尝试开发一个 python 脚本来使用 GMail 帐户发送电子邮件,但似乎遇到了 SSL 证书问题。我启用了允许安全性较低的应用程序访问我的 Gmail 帐户的选项。任何人都可以帮助我确定我做错了什么,因为我收到此错误:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:997) - 详细的堆栈跟踪是以下。

import smtplib, ssl


class Mail:

    def __init__(self):
        self.port = 465
        self.smtp_server_domain_name = "smtp.gmail.com"
        self.sender_mail = "[email protected]"
        self.password = "Password@123"

    def send(self, emails, subject, content):
        ssl_context = ssl.create_default_context()
        service = smtplib.SMTP_SSL(self.smtp_server_domain_name, self.port, context=ssl_context)
        service.login(self.sender_mail, self.password)

        for email in emails:
            result = service.sendmail(self.sender_mail, email, f"Subject: {subject}\n{content}")

        service.quit()


if __name__ == '__main__':
    mails = input("Enter emails: ").split()
    subject = input("Enter subject: ")
    content = input("Enter content: ")

    mail = Mail()
    mail.send(mails, subject, content)

堆栈跟踪如下:

Traceback (most recent call last):
  File "/Users/prashanth/PycharmProjects/pythonTools/Mail.py", line 29, in <module>
    mail.send(mails, subject, content)
  File "/Users/prashanth/PycharmProjects/pythonTools/Mail.py", line 14, in send
    service = smtplib.SMTP_SSL(self.smtp_server_domain_name, self.port, context=ssl_context)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1050, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1057, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1070, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)

Process finished with exit code 1

我的环境:

MacOS Big Sur 11.6.5
Python : 3.10

I am trying to develop a python script to send an email using a GMail account and seem to be encountering an SSL Certificate issue. I have enabled the option to let less secure apps access my gmail account. Could anyone please help me to identify what I am doing wrong as I am getting this error:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997) - Detailed stack trace is below.

import smtplib, ssl


class Mail:

    def __init__(self):
        self.port = 465
        self.smtp_server_domain_name = "smtp.gmail.com"
        self.sender_mail = "[email protected]"
        self.password = "Password@123"

    def send(self, emails, subject, content):
        ssl_context = ssl.create_default_context()
        service = smtplib.SMTP_SSL(self.smtp_server_domain_name, self.port, context=ssl_context)
        service.login(self.sender_mail, self.password)

        for email in emails:
            result = service.sendmail(self.sender_mail, email, f"Subject: {subject}\n{content}")

        service.quit()


if __name__ == '__main__':
    mails = input("Enter emails: ").split()
    subject = input("Enter subject: ")
    content = input("Enter content: ")

    mail = Mail()
    mail.send(mails, subject, content)

Stacktrace is below:

Traceback (most recent call last):
  File "/Users/prashanth/PycharmProjects/pythonTools/Mail.py", line 29, in <module>
    mail.send(mails, subject, content)
  File "/Users/prashanth/PycharmProjects/pythonTools/Mail.py", line 14, in send
    service = smtplib.SMTP_SSL(self.smtp_server_domain_name, self.port, context=ssl_context)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1050, in __init__
    SMTP.__init__(self, host, port, local_hostname, timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 1057, in _get_socket
    new_socket = self.context.wrap_socket(new_socket,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 512, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1070, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1341, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)

Process finished with exit code 1

My Environment:

MacOS Big Sur 11.6.5
Python : 3.10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文