"Connection unexpectedly closed" 使用来自 AWS 的 SES 发送电子邮件时出错

"Connection unexpectedly closed" error while sending email using SES from AWS

所以我正在尝试使用 SMTP 和 AWS 向自己发送电子邮件。我在配置中使用的电子邮件已经过验证,因为我仍在 SES 中使用沙盒模式。虽然 运行 脚本我一直收到错误 Connection unexpectedly closed 甚至面团我尝试连接 OpenSSL 并且它连接但它在连接后显示 Didn't find STARTTLS in server response, trying anyway... 错误。

这是我的代码:

MAIL = {}
MAIL['USERNAME'] = 'AKIAXARHTFGFKCDAO7PD'
MAIL['PASSWORD'] = 'BE0tXPq8wteiKZYtgD2TgtfFTGhgFGOhUp3F0lG0uqn'
MAIL['HOST'] = 'email-smtp.eu-central-1.amazonaws.com'
MAIL['PORT'] = 465


# Set user code
            code = random.randrange(000000, 999999)
            # Send email to user
            print(code)
            print(current_user.email)
            msg = MIMEMultipart('alternative')
            msg['Subject'] = 'Ruby - Verification code'
            msg['From'] = 'amng835@gmail.com'
            msg['To'] = current_user.email
            msg.attach(MIMEText(f'Your verification code is: {code}', 'plain'))
            try:
                server = smtplib.SMTP(MAIL['HOST'], MAIL['PORT'])
                server.ehlo()
                server.starttls()
                server.ehlo()
                server.login(MAIL('MAIL_USERNAME'), MAIL('MAIL_PASSWORD'))
                server.sendmail('amng835@gmail.com', current_user.email, msg.as_string())
                server.close()
            except Exception as error:
                print('Failed to send message to user')
                print(error)

OpenSSL 输出:

命令:

openssl s_client -connect email-smtp.eu-central-1.amazonaws.com:465 -starttls smtp

输出:

CONNECTED(00000005)
Didn't find STARTTLS in server response, trying anyway...
write:errno=0
---
no peer certificate available
---
No client certificate CA names sent
---
SSL handshake has read 0 bytes and written 372 bytes
Verification: OK
---
New, (NONE), Cipher is (NONE)
Secure Renegotiation IS NOT supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
Early data was not sent
Verify return code: 0 (ok)
---

我的文档来源: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-smtp.html

端口 465.Change 似乎有一些问题,下面的代码可以正常工作。

MAIL['PORT'] = 587