Rom*_*rik 7 python email ssl smtplib python-3.x
这是一个关于通过经过身份验证的SMTP(而非gmail)发送电子邮件的问题.下面的脚本通过本网站上的各种问题和答案汇总在一起,但是我得到的错误是没有"googlable"候选人用于这个特定的工具组合.我在Python 3.5.1中工作,它产生了这个错误:
邮件失败; [SSL: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))
Run Code Online (Sandbox Code Playgroud)
感谢我的问题下的两位评论员.通过设置导航SSL后
from smtplib import SMTP as SMTP
Run Code Online (Sandbox Code Playgroud)
创建STMP对象后启用TLS(如果我没有使用正确的术语,请原谅我)
conn = SMTP(host=SMTPserver, port=587) # object created
conn.ehlo()
conn.starttls() # enable TLS
conn.ehlo()
Run Code Online (Sandbox Code Playgroud)
我能够发送电子邮件.
我有同样的问题.我刚改变自:
EMAIL_USE_SSL = True
Run Code Online (Sandbox Code Playgroud)
至:
EMAIL_USE_TLS = True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12703 次 |
| 最近记录: |