<i id='GWmlP'><tr id='GWmlP'><dt id='GWmlP'><q id='GWmlP'><span id='GWmlP'><b id='GWmlP'><form id='GWmlP'><ins id='GWmlP'></ins><ul id='GWmlP'></ul><sub id='GWmlP'></sub></form><legend id='GWmlP'></legend><bdo id='GWmlP'><pre id='GWmlP'><center id='GWmlP'></center></pre></bdo></b><th id='GWmlP'></th></span></q></dt></tr></i><div id='GWmlP'><tfoot id='GWmlP'></tfoot><dl id='GWmlP'><fieldset id='GWmlP'></fieldset></dl></div>

        • <bdo id='GWmlP'></bdo><ul id='GWmlP'></ul>
        <legend id='GWmlP'><style id='GWmlP'><dir id='GWmlP'><q id='GWmlP'></q></dir></style></legend>

        <tfoot id='GWmlP'></tfoot>
      1. <small id='GWmlP'></small><noframes id='GWmlP'>

        使用 Python smtplib 发送没有密钥文件(仅证书文件)的电子邮件

        时间:2023-07-03
          <tbody id='7JxYH'></tbody>
      2. <small id='7JxYH'></small><noframes id='7JxYH'>

        <legend id='7JxYH'><style id='7JxYH'><dir id='7JxYH'><q id='7JxYH'></q></dir></style></legend>
            <tfoot id='7JxYH'></tfoot>

              • <bdo id='7JxYH'></bdo><ul id='7JxYH'></ul>
                <i id='7JxYH'><tr id='7JxYH'><dt id='7JxYH'><q id='7JxYH'><span id='7JxYH'><b id='7JxYH'><form id='7JxYH'><ins id='7JxYH'></ins><ul id='7JxYH'></ul><sub id='7JxYH'></sub></form><legend id='7JxYH'></legend><bdo id='7JxYH'><pre id='7JxYH'><center id='7JxYH'></center></pre></bdo></b><th id='7JxYH'></th></span></q></dt></tr></i><div id='7JxYH'><tfoot id='7JxYH'></tfoot><dl id='7JxYH'><fieldset id='7JxYH'></fieldset></dl></div>

                  本文介绍了使用 Python smtplib 发送没有密钥文件(仅证书文件)的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  尝试使用以下脚本发送带有证书文件的电子邮件:

                  Trying to send email with a certificate file using the following script:

                  import smtplib
                  
                  client = smtplib.SMTP(myhost, myport)
                  client.ehlo()
                  client.starttls(certfile=mycertfile)
                  client.ehlo()
                  
                  client.login(myusername, mypassword)
                  client.sendmail(sender, receiver, Message)
                  client.quit()
                  

                  我收到以下错误:

                  SSLError: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib
                  

                  我认为文档(smtplib.html 和ssl.html) 说我需要提供一个私钥.我只有证书文件(base64 PEM 格式).我的 devops 说在这种情况下不需要私钥,因为我不需要识别连接的本地端.

                  I think documentations (smtplib.html and ssl.html) say I need to provide a private key. I only have the certificate file (base64 PEM format). My devops says that a private key is not required in this case because I do not need to identify the local side of the connection.

                  有没有办法在不提供私钥的情况下发送电子邮件?如果需要私钥,为什么?

                  Is there a way to send the email without providing the private key? If a private key is required, why?

                  推荐答案

                  有两种使用 SSL/TLS 的方法:客户端经过身份验证和客户端未经身份验证的基本".在客户端经过身份验证的连接中,服务器和客户端都向对方发送证书.在基本"中,只有服务器可以.

                  There are two ways to use SSL/TLS: client authenticated and "basic" where the client is unauthenticated. In client authenticated connections, the both the server and the client send a certificate to the other. In "basic" only the server does.

                  如果您既没有传递证书也没有传递密钥文件,smtplib 将使用基本连接,在此对客户端进行身份验证.

                  If you pass neither a certificate nor a keyfile, smtplib will use a basic connection, where the client is authenticated.

                  如果您使用证书,它将用于经过客户端身份验证的连接.在这种情况下,服务器将要求客户端通过签署握手消息来表明它拥有证书.为了让客户端能够做到这一点,它还需要私钥,它可以在证书文件中,也可以作为单独的密钥文件.

                  If you use a certificate, it will be used for a client authenticated connection. In that case, the server will demand that the client shows it owns the certificate by signing a handshake message. For the client to be able to do that it also needs the private key, which can be either in the certificate file or as a separate keyfile.

                  长话短说,如果要使用客户端证书,还必须使用密钥.如果没有,您可以将两者都留空.

                  Long story short, if you want to use a client certificate, you must also use a key. If not, you can just leave both empty.

                  OTOH,也许您有要用于连接的服务器证书文件或 CA 列表?

                  OTOH, maybe you have a server certificate file or CA list you want to use with the connection?

                  在这种情况下,您需要将其传递给 ca_certs 参数中的 ssl.wrap_socket.由于您使用 Python 2.6,因此使用 smtplib 并没有简单的方法来做到这一点(Python 3.3+ 对 context 参数.org/3.4/library/smtplib.html#smtplib.SMTP.starttls" rel="nofollow">starttls).

                  In that case you need to pass it to ssl.wrap_socket in the ca_certs parameter. Since you use Python 2.6 there's no easy way to do that with smtplib (Python 3.3+ has a context argument to starttls).

                  如何解决这个问题取决于您的应用程序.例如,如果您不需要 ssl 来做其他任何事情,一个骇人听闻的解决方案是将 ssl.wrap_socket 与提供您的 ca_cert(可能还有 cert_reqs=CERT_REQUIRED).

                  How to solve this depends on your application. For example, if you do not need ssl for anything else, a hackish solution would be to monkey-patch ssl.wrap_socket with one that provides your ca_cert (as well as cert_reqs=CERT_REQUIRED, likely).

                  更完整的解决方案是使用您自己的允许传入这些参数的变体扩展 smtplib.SMTP.

                  A more full blown solution would be to extend smtplib.SMTP with your own variant that does allow passing in those parameters.

                  这篇关于使用 Python smtplib 发送没有密钥文件(仅证书文件)的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 SMTPLib Python 时获取未经授权的发件人地址 下一篇:Python 的 SMTP AUTH 扩展问题

                  相关文章

                1. <tfoot id='Oeq7H'></tfoot>
                2. <small id='Oeq7H'></small><noframes id='Oeq7H'>

                  <i id='Oeq7H'><tr id='Oeq7H'><dt id='Oeq7H'><q id='Oeq7H'><span id='Oeq7H'><b id='Oeq7H'><form id='Oeq7H'><ins id='Oeq7H'></ins><ul id='Oeq7H'></ul><sub id='Oeq7H'></sub></form><legend id='Oeq7H'></legend><bdo id='Oeq7H'><pre id='Oeq7H'><center id='Oeq7H'></center></pre></bdo></b><th id='Oeq7H'></th></span></q></dt></tr></i><div id='Oeq7H'><tfoot id='Oeq7H'></tfoot><dl id='Oeq7H'><fieldset id='Oeq7H'></fieldset></dl></div>
                  <legend id='Oeq7H'><style id='Oeq7H'><dir id='Oeq7H'><q id='Oeq7H'></q></dir></style></legend>

                  • <bdo id='Oeq7H'></bdo><ul id='Oeq7H'></ul>