Python 连接到 smtp 端口 465 服务器失败

Python connetion to smtp port 465 server fails

这是我的脚本:

import smtplib
from socket import gaierror

port = 465 
smtp_server = "my_server.com"
login = "my_login_name.com"
password = "my_pass"

sender = "my_email.com"
receiver = "receiver_email.com"

message = f"""Subject: Hi Mailtrap
To: {receiver}
From: {sender}

This is my first message with Python."""

try:
    with smtplib.SMTP(smtp_server, port) as server:
        server.login(login, password)
        server.sendmail(sender, receiver, message)

    print('Sent')
except (gaierror, ConnectionRefusedError):
    print('Failed to connect to the server. Bad connection settings?')
except smtplib.SMTPServerDisconnected:
    print('Failed to connect to the server. Wrong user/password?')
except smtplib.SMTPException as e:
    print('SMTP error occurred: ' + str(e))

我正在尝试发送简单的电子邮件,但我收到了回复消息:Failed to connect to the server. Wrong user/password?。我想与服务器的连接存在一些问题。端口号、SMTP 服务器地址、登录名和密码正确。我还尝试使用我的 google 电子邮件帐户和 google SMTP 服务器,但回答相同。什么东西少了?谢谢!

尝试使用ssl smtplib.SMTP_SSL(主机,465)