2

Im having trouble verifying a mass list of email addresses. The issue is with the error code "connection refused" Every email verification returns Connection refused. Why would this be? Can you give me a solution? No issues with verifying the email syntax The below code is only for the correct area of the programme, i.e mxrecord check. Im using intellij Idea, python 3.4.3 and tkinter for the 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

Thanks in advance.

0 Answers0