python - 邮件失败; [SSL : UNKNOWN_PROTOCOL] unknown protocol (_ssl. c:645)

标签 python email python-3.x ssl smtplib

这是一个关于通过经过身份验证的 SMTP(不是 gmail)发送电子邮件的问题。下面的脚本是通过本网站上的各种问题和答案组合在一起的,但我收到一个错误,没有针对此特定工具组合的“可搜索”候选者。我在 Python 3.5.1 中工作,它产生了这个错误:

mail failed; [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:645)

这是客户端错误还是服务器错误?我是否遗漏了一些我不知道的证书? AFAIK 服务器支持 SSL 身份验证。任何朝着正确方向的想法和插入都将不胜感激。

import sys
from smtplib import SMTP_SSL as SMTP
from email.mime.text import MIMEText

# credentials masked, obviously
SMTPserver = 'myserver'
sender = 'mymail'
destination = ['recipient']

USERNAME = "myusername"
PASSWORD = "mypass"

# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'

content = """\
Test message
"""

subject = "Sent from Python"

try:
    msg = MIMEText(content, text_subtype)
    msg['Subject'] = subject
    msg['From'] = sender
    conn = SMTP(host=SMTPserver, port=465)
    conn.set_debuglevel(False)
    conn.login(USERNAME, PASSWORD)

    try:
        conn.sendmail(sender, destination, msg.as_string())
    finally:
        conn.quit()

except Exception as exc:
    sys.exit("mail failed; %s" % str(exc))

最佳答案

感谢我问题下的两位评论员。通过设置浏览 SSL 之后

from smtplib import SMTP as SMTP 

并在创建 STMP 对象后启用 TLS(如果我使用的术语不正确,请原谅我)

conn = SMTP(host=SMTPserver, port=587)  # object created
conn.ehlo() 
conn.starttls()  # enable TLS
conn.ehlo()

我能够发送电子邮件。

enter image description here

关于python - 邮件失败; [SSL : UNKNOWN_PROTOCOL] unknown protocol (_ssl. c:645),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241273/

相关文章:

python - 双轴上的刻度和标签

Python - 使用多个值模拟对象的属性?

python - future X 天生日的人的查询集

python - __iter__ 和 __getitem__ 有什么区别?

php - 通过 SendGrid 服务发送电子邮件的简单 PHP 类

c# - 发送保存在磁盘上的 eml 文件

python - 包上的 `del` 有某种内存

python - 仅在 django 模板中显示唯一对象

python - 使用按键在 QLineEdit 中输入文本并在 PyQt4 中发出信号

c# - 如何在 C# 中将 RAR 文件附加到电子邮件?