在学习《RabbitMQ实战》这本书,书中是代码为python语言,在用书中例子发送qq邮件时,遇到以下错误
报错信息:ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
def send_mail(recipients,subject,message):
"""E-mail generator for received alerts."""
headers=("From:%s\r\nTo: \r\nDate: \r\n"+ \
"Subject: %s\r\n\r\n") % ("[email protected]",subject)
smtp_server=smtplib.SMTP_SSL("smtp.qq.com","587")
smtp_server.ehlo()
smtp_server.login("[email protected]","授权码")
smtp_server.sendmail("[email protected]",recipients,headers+str(message))
smtp_server.close()
后来在网上查到说是端口不正确,但是QQ邮箱smtp服务器ssl连接方式的端口为465或587,官网有写,那我用587应该没错啊,但就是不行,最后改成465就OK了
解决方法:将端口换成465,smtp_server=smtplib.SMTP_SSL("smtp.qq.com","465")