首页 > 解决方案 > 检查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))

标签: python

解决方案


推荐阅读