我正在尝试使用这种方法(请参阅下文)来验证电子邮件地址,这种方法在无数不同的地方以一种或另一种形式被引用和推荐。问题是当我进入“ server.connect(mxRecord)”部分时,它停顿并超时了100%。
我尝试过:
我的设置:
import dns.resolver
import smtplib
import socket
addressToVerify = 'rickymartin@yahoo.com'
domainToVerify = 'yahoo.com'
records = dns.resolver.query(domainToVerify, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
print(mxRecord)
server = smtplib.SMTP(timeout=20)
server.set_debuglevel(0)
server.connect(mxRecord)
server.helo(server.local_hostname)
server.mail('me@domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
print('Valid')
else:
print('Invalid')
这是我收到的错误:
Traceback (most recent call last):
File "email-validator.py", line 17, in <module>
server.connect(mxRecord)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
socket.timeout: timed out