python连接到SMTP端口465服务器失败

时间:2020-07-02 08:34:18

标签: python smtp

这是我的脚本:

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服务器,但是回答相同。什么东西少了?谢谢!

1 个答案:

答案 0 :(得分:0)

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