【发布时间】:2021-02-23 12:54:44
【问题描述】:
我之前遇到过一个问题,我收到了ValueError: server_hostname cannot be an empty string or start with a leading dot。在 Stack Overflow 中检查了this post,当我按照说明在控制台中运行时:
import smtplib
smtplib.SMTP_SSL(host='gmail-smtp-in.l.google.com').connect(host='gmail-smtp-in.l.google.com', port=25)
但是我得到了一个超时,如下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1040, in _get_socket
new_socket = socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
仅在我的代码 server.starttls() 中启用时遇到问题。如果注释掉,它可以正常发送电子邮件。
下面是我的代码的 sn-p:
server = smtplib.SMTP_SSL()
server.connect('gmail-smtp-in.l.google.com', 25) # mail server want to connect to.
server.set_debuglevel(True) # shows communication with the server in the console
server.ehlo()
server.starttls()
server.ehlo()
【问题讨论】:
-
3.8也可能有这个bug,不如试试他们在其他帖子里说的吧?
-
我尝试了另一篇文章中的建议,但它对我不起作用。仍然得到同样的错误
-
您上面的代码没有显示。那么我们该如何提供帮助呢?发布minimal reproducible example
-
@Tomerikoo
server = smtplib.SMTP_SSL(host = 'smtp.gmail.com').connect(host ='smtp.gmail.com',port = 465) # mail server we want to connect to. server.set_debuglevel(True) # shows communication with the server in the console server.ehlo() server.starttls() server.ehlo()它挂起并且不发送电子邮件,而之前它抛出了一个错误 - 我也在控制台中尝试了import smtplib;smtplib.SMTP_SSL(host='smtp.gmail.com').connect(host='smtp.gmail.com', port=465)和相同的行为,它挂起
标签: python