这是一个关于通过经过身份验证的 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()
我能够发送电子邮件。
关于python - 邮件失败; [SSL : UNKNOWN_PROTOCOL] unknown protocol (_ssl. c:645),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36241273/