Checking email address in java

How exactly do I validate the domain portion of an email address? Do I need to list the existing domains in my java class first, or will java InternetAddress.validate()

execute it by default? I used this:

public static boolean verifyEmailAddress(String regEmail) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(regEmail);
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    return result;
}

      

Request.getParameter has an email address and is stored in regEmail. The problem is that even for invalid emails such as san@hhhgggmail.com

, it shows what is valid. What exactly do I need to do. Help me. And also does this feature work great for those who have used and tested it?

+3


source to share


4 answers


I think you are approaching the problem from the wrong point of view. From your application point of view, an email should be considered valid (better, useful) if it can receive mail. That's why all of these forums keep sending you activation email. You have to send some random string to each new email address and keep it in quarantine until the user can prove they are reading the secret.



This is because a domain may exist, or even an MX record for that domain may exist in DNS, but none of these conditions can guarantee that the address is valid - again when you confirm something, you are indeed claiming that it can be used later in your code for some purpose, and the purpose for an email address is to receive mail

+3


source


I don't know if there is an automatic way in Java. But I would look for the domain MX record. If an MX record exists, the domain can potentially receive mail.



For more information see this page .

+1


source


Why aren't you using InetAddres.getByName in the domain part?

+1


source


I think there is no effective way to refute this. all we can do is disprove the pattern, or more you trust an email domain like hhhgggmail.com. but how could you disprove that " san@hhhgggmail.com really exists"?

SMTP do has a "VEFY" command, but almost all smtp server does not execute this command for security reasons.

oh, you want the vefy domain. all SMTP servers need an mx dns entry. you can use dnsjava module to test it. code like:

public static MXRecord digOptimalMxRecords(String domainName) throws TextParseException {
        List<MXRecord> records = DNSHelper.digMxRecords(domainName);
        if (CollectionUtils.isNotEmpty(records)) {
            Collections.sort(records, new Comparator<MXRecord>() {
                @Override
                public int compare(MXRecord o1, MXRecord o2) {
                    return o2.getPriority() - o1.getPriority();
                }
            });
            return records.get(0);
        }
        return null;
    }

public static List<MXRecord> digMxRecords(String domainName) throws TextParseException {
    List<MXRecord> list = new ArrayList<MXRecord>();
    Lookup mxLookupper = new Lookup(domainName, Type.MX);
    mxLookupper.run();
    if (mxLookupper.getResult() == Lookup.SUCCESSFUL) {
        Record[] answers = mxLookupper.getAnswers();
        for (Record record : answers) {
            list.add((MXRecord) record);
        }
    }
    return list;
}

      

0


source







All Articles