Python smtp.connect 无法连接

Xen*_*cus 4 python email smtp

我正在编写一个小的 python 脚本来发送电子邮件,但我的代码甚至无法通过 smtp 连接。我正在使用以下代码,但我从未看到“已连接”。此外,我没有看到抛出异常,所以我认为它挂在某些东西上。

import os
import platform
import smtplib

#Define our SMTP server. We use gmail. Try to connect.
try:
    server = smtplib.SMTP()
    print "Defined server"
    server.connect("smtp.gmail.com",465)
    print "Connected"
    server.ehlo()
    server.starttls()
    server.ehlo()
    print "Complete Initiation"
except Exception, R:
    print R
Run Code Online (Sandbox Code Playgroud)

Ant*_*ala 7

端口 465 用于 SMTPS;要连接到 SMTPS,您需要使用 SMTP_SSL;但是SMTPS 已被弃用,您应该使用 587(带有starttls)。(另请参阅有关 SMTPS 和 MSA 的答案)。

其中任何一个都可以工作: 587 与starttls

server = smtplib.SMTP()
print("Defined server")
server.connect("smtp.gmail.com",587)
print("Connected")
server.ehlo()
server.starttls()
server.ehlo()
Run Code Online (Sandbox Code Playgroud)

465 与SMTP_SSL.

server = smtplib.SMTP_SSL()
print("Defined server")
server.connect("smtp.gmail.com", 465)
print("Connected")
server.ehlo()
Run Code Online (Sandbox Code Playgroud)

  • 当我在我的个人计算机中执行前面的代码时,它工作正常,但是,如果我尝试在我的虚拟机(工作中,SUSE Linux Enterp Srv 11 SP4)中运行它,它永远不会连接,而是抛出连接超时错误,有帮助吗?**我没有管理员权限** (2认同)