0

当我用它测试下面的代码时,server = smtplib.SMTP('smpt.gmail.com:587')它工作正常。

但是当我将 SMTP 服务器更改为server = smtplib.SMTP('10.10.9.9: 25')- 它给了我一个错误。此 SMTP 不需要任何密码。

那么我在这里错过了什么?

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas as pd

def send_email(user, recipient, subject):
    try:
        d = {'Col1':[1,2], 'Col2':[3,4]}
        df=pd.DataFrame(d)
        df_html = df.to_html()
        dfPart = MIMEText(df_html,'html')

        user = "myEmail@gmail.com"
        #pwd = No need for password with this SMTP
        subject = "Test subject"
        recipients = "some_recipientk@blabla.com"
        #Container
        msg = MIMEMultipart('alternative')
        msg['Subject'] = subject
        msg['From'] = user
        msg['To'] = ",".join(recipients)
        msg.attach(dfPart)

        #server = smtplib.SMTP('smpt.gmail.com:587') #this works
        server = smtplib.SMTP('10.10.9.9: 25') #this doesn't work
        server.starttls()
        server.login(user, pwd)

        server.sendmail(user, recipients, msg.as_string())
        server.close()
        print("Mail sent succesfully!")
    except Exception as e:
        print(str(e))
        print("Failed to send email")
send_email(user,"","Test Subject")
4

2 回答 2

1

如果服务器不需要身份验证,那么不要使用 SMTP AUTH。

删除以下行:
server.login(user, pwd)

于 2019-06-08T06:22:27.083 回答
1

嗨,我不完全确定它为什么不起作用,但我有一些事情可以检查。

  • server = smtplib.SMTP('10.10.9.9: 25')
    你在 ip:port 字符串中有一个空格,尝试删除它。

  • ip:port 组合似乎来自私有 LAN 地址
    尝试 ping 此地址以查看是否可以访问它,如果无法访问,请与在您的网络中使用给定 ip 处理机器的人交谈。

    如果你能ping通IP,那么有可能SMTP服务器在给定的端口上不可用,在这种情况下也可以联系负责管理IP的机器的人:10.10.9.9

    在终端
    ping 10.10.9.9上使用给定的命令


  • 同样在登录和发送邮件之前,您应该使用 connect() 连接到服务器,正确的顺序是。

    server = smtplib.SMTP('10.10.9.9: 25')
    server.starttls()
    server.connect('10.10.9.9', 465)
    server.login(user, pwd)
    server.sendmail(user, recipients, msg.as_string ())
    server.close()

465 是 SMTP 服务器的默认端口

谢谢,
如果对您有帮助,请告诉我!

于 2019-06-08T06:57:34.163 回答