Python 3群发电子邮件验证连接被拒绝

时间:2018-04-27 11:21:15

标签: python python-3.x error-code email-verification connection-refused

我无法验证电子邮件地址的大量列表。问题在于错误代码"连接被拒绝"每封电子邮件验证都会返回Connection refused为什么会这样?你能给我一个解决方案吗?验证电子邮件语法没有问题 以下代码仅适用于程序的正确区域,即mxrecord检查。我使用intellij Idea,python 3.4.3和tkinter进行GUI。

def handle_accuracy(self):
    for email in self.emails:
        # See if email string contains ampersand and period.
        if (email.find('@') < 0) or (email.find('.') < 0):
            print("Email not syntactically correct.")
            self.inaccurate_emails.append(email)
        else:
            email_exists = self.check_existence(email)
            if email_exists:
                print("Email is accurate and exists.")
                self.accurate_emails.append(email)
            else:
                print("Email is syntactically correct but couldn\'t be found")
                self.inaccurate_emails.append(email)

def check_existence(self, email):
    at_pos = email.find('@')
    mx_name = email[(at_pos + 1):]

    # Connect to email server and get name of SMTP server
    records = dns.resolver.query(mx_name, 'MX')

    for record in records:
        print(record.exchange)

    mxRecord = records[0].exchange
    mxRecord = str(mxRecord)

    host = socket.gethostname()

    # Setup an exception block to handle issues with connection.
    try:
        server = smtplib.SMTP(mxRecord)
    except TimeoutError:
        print("Timeout")

        # Indicate to calling function that email cannot be found.
        return False
    except ConnectionRefusedError:
        print("Connection Refused")
        return False
    server.set_debuglevel(0)

    # Setup another exception block to handle further issues with connection.
    try:
        server.connect()
        server.helo(host)   #needs to have a helo rather than hello
        server.mail(email)
        code, message = server.rcpt()
    except TimeoutError:
        print("Timeout")
        server.quit()
        return False
    except ConnectionRefusedError:
        print("Connection Refused")
        server.quit()
        return False

    server.quit()

    if code == 250:
        return True
    else:
        return False

提前致谢。

0 个答案:

没有答案