从 Python 发送电子邮件时抛出 SSLError 'unknown protocol'

Sending email from Python throwing SSLError 'unknown protocol'

我从 flask-mail 发送电子邮件,但由于尝试使用 namecheap 或 bluehost 的邮件服务器,我收到以下错误:

SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

所以现在我尝试在不使用 flask-mail 的情况下发送电子邮件,但我仍然遇到同样的错误。任何修复? 我的代码如下:

from smtplib import SMTP
smtp = SMTP()

smtp.set_debuglevel(debuglevel)
smtp.connect('xxxxxx', 26)
smtp.login('noreply@xxx.com', 'xxxxxxx')

from_addr = "xxx <noreply@xxx.com>"
to_addr = rec@xxx.com

subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )

message_text = "Hello\nThis is a mail from your server\n\nBye\n"

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

我的应用程序是 运行 上 Ubuntu 14.04 on Amazon EC2。

谢谢。

给您这个错误的原因是您的邮件服务器不是 SMTP 服务器。使用 Gmail 或其他 smtp 邮件服务发送邮件。尝试通过服务器为 smtp.gmail.com 且端口为 587 的 gmail 帐户发送它。首先,您需要 configure 您的帐户。