socket.error:[Errno 10060]:无法使用Python从Outlook发送电子邮件

时间:2018-07-31 12:26:06

标签: python python-2.7 email outlook ms-office

我下面的代码可以正常工作几个月。现在,它给了我下面的错误,

  

socket.error:[Errno 10060]连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应而建立连接失败

        sender = 'myemail@email.com'
        receiver = ['myemail@email.com',]
        msg = MIMEMultipart()

        print("Sending an email to {} ...".format(receiver))

        msg['From'] = sender
        msg['To'] = ", ".join(receiver)
        msg['Subject'] = 'Automation Testing'

        body = 'Hello,' 
        msg.attach(MIMEText(body, 'plain'))
        # filename = 'Fidelity-Success' + timestamp + '.png'
        attachment = open('C:\\mydrive\\{}'.format(filename), 'rb')

        part = MIMEBase('application', 'octet-stream')
        part.set_payload((attachment).read())
        # part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= " + filename)

        msg.attach(part)

        server = smtplib.SMTP('mail.email.com', 587, timeout=120)
        #server = smtplib.SMTP_SSL('mail.email.com', 93, timeout=120)

        server.starttls()
        server.login(sender, "password")
        text = msg.as_string()
        server.sendmail(sender, receiver, text)
        # server.quit()
        time.sleep(4)
        print("Email sent.")

我尝试了各种端口和超时,但是没有运气。我不确定为什么它突然停止工作。

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

存在网络问题。我只需要添加邮件和smtp服务器的IP。

临时修订:在管理员模式下的命令提示符下运行以下命令:

route add 'IP of mail server' mask 255.255.255.255 'default gateway of your machine'
route add 'IP of smtp server' mask 255.255.255.255 'default gateway of your machine'

永久性修复:请网络团队将邮件服务器的IP添加到拆分隧道中。