检查python中是否存在电子邮件地址

时间:2018-08-23 06:19:34

标签: python

我想检查python 2.7中是否存在电子邮件地址。我使用了SMTP服务器,但未返回任何结果。 有没有人想检查电子邮件地址是否存在?

try:
    email_address = 'me@exemple.com'
    domain_name = email_address.split('@')[1]
    records = dns.resolver.query(domain_name, 'MX')
    mxRecord = records[0].exchange
    mxRecord = str(mxRecord)

    #Step 3: ping email server
    #check if the email address exists

    # Get local server hostname
    host = socket.gethostname()

    # SMTP lib setup (use debug level for full output)
    server = smtplib.SMTP()
    server.set_debuglevel(0)

    # SMTP Conversation
    server.connect(mxRecord)
    server.helo(host)
    server.mail('me@domain.com')
    code, message = server.rcpt(str(addressToVerify))
    server.quit()
    print(code)
    # Assume 250 as Success
    if code == 250:
        print('Y')
    else:
        print('N')
except Exception as error:
    print('Erreur:'+repr(error))

0 个答案:

没有答案