Python 使用 ipaddress 发送电子邮件

Python send emails using ipaddress

我正在尝试通过 Python 发送电子邮件,使用 SMTP 中继,我无法从我的 Outlook 帐户访问真正的 SMTP,我的 IP 地址已添加到 smtp 中继服务器。我试图 运行 我的代码只使用我的 ipaddress 但失败了。

错误ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标机器主动拒绝

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
 
# create message object instance
msg = MIMEMultipart()
 
 
message = "Thank you"
 
# setup the parameters of the message
password = "myPassword"
msg['From'] = "myemail@gmail.com"
msg['To'] = "myemail@gmail.com"
msg['Subject'] = "Subscription"
 
# add in the message body
msg.attach(MIMEText(message, 'plain'))
 
#create server
server = smtplib.SMTP('Here I put my Ip address')  
 
server.starttls()
 
# Login Credentials for sending the mail
server.login(msg['From'], password)
 
 
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
 
server.quit()
 
print ("successfully sent email to %s:" % (msg['To']))

此致

中继的 IP 地址和端口应该在这里: server = smtplib.SMTP('RELAY_IP_HERE', RELAY_SMTP_PORT_HERE)

另外,它可能被中继机器上的防火墙阻止了。如果您有服务器的管理员权限,您可以尝试检查 SMTP 服务。您可以尝试使用 telnet 检查端口是否打开。这是一个 link,它描述了如何使用来自 windows 机器的 telnet 执行检查: https://support.hostway.com/hc/en-us/articles/360002236820-How-to-Test-SMTP-Services-Manually-in-Windows-Server