3

我究竟如何验证电子邮件地址的域部分?我是否需要首先列出我的 java 类中的现有域,或者 javaInternetAddress.validate()将默认执行它?我用过这个:

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 有一个电子邮件地址并存储在 regEmail 中。问题甚至是无效的电子邮件,比如san@hhhgggmail.com它显示有效。谁使用过并测试过?

4

5 回答 5

3

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

This is because the domain could exist, or even an MX record for that domain can exist in DNS, but neither of these conditions can guarantee that the address is valid - again, when you validate something you are really stating that it can be used later in your code for some purpose, and the purpose for an email address is to receive mail

于 2012-04-07T21:30:09.190 回答
1

我不知道Java中是否有自动方式。但我会查找域的 MX 记录。如果存在 MX 记录,则域可能会收到邮件。

另请参阅此页面了解更多信息。

于 2012-04-07T05:40:34.723 回答
1

为什么不在域部分使用InetAddres.getByName呢?

于 2012-04-07T05:48:22.350 回答
0

我认为没有完全有效的方法来验证它。我们所能做的就是验证模式,或者更多地验证邮件域,例如 hhhgggmail.com。但是你怎么能怀疑“san@hhhgggmail.com 确实存在”?

SMTP 确实有一个命令“VEFY”,但出于安全原因,几乎所有的 smtp 服务器都没有实现这个命令。

哦,你想验证域名。所有 smtp 服务器都需要一个 mx dns 记录。您可以使用 dnsjava 模块来验证它。代码为:</p>

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;
}
于 2012-04-09T03:27:27.403 回答
0

请忽略变量名称,因为它们不专业:

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import java.util.Scanner;

public class EmailValidator {

public static void main(String[] args) {

    recursion();

}

static void recursion () {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Welcome to the game of typing a correct email address!");
    
    System.out.println();
    
    System.out.println("As the game says, enter a correct email address.");

    System.out.println();

    String snow = scanner.nextLine();

    String[] sssp = snow.split("@");
    
    if (sssp.length != 2) {
        
    System.out.println();
        
    System.out.println("This is not a correct email address."); 
    
    System.out.println();
        
    recursion();
    
    } else {

        String regex = "[^a-z0-9._]";

        String regex1 = "^\\.|\\.$";
        
        String regex2 = "\s";
        
        String regex3 = "^$";
        
        String regex7 = "\\._|_\\.";
        
        String regex39 = "__";
        
        String regex19 = "\\.\\.";
        
        String regex20 = "^_|_$";

        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

        Pattern pattern1 = Pattern.compile(regex1, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern2 = Pattern.compile(regex2, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern3 = Pattern.compile(regex3, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern11 = Pattern.compile(regex7, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern12 = Pattern.compile(regex19, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern13 = Pattern.compile(regex20, Pattern.CASE_INSENSITIVE);
        
        Pattern pattern14 = Pattern.compile(regex39, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pattern.matcher(sssp[0]);

        Matcher matcher1 = pattern1.matcher(sssp[0]);
        
        Matcher matcher2 = pattern2.matcher(sssp[0]);
        
        Matcher matcher3 = pattern3.matcher(sssp[0]);
        
        Matcher matcher11 = pattern11.matcher(sssp[0]);
        
        Matcher matcher12 = pattern12.matcher(sssp[0]);
        
        Matcher matcher13 = pattern13.matcher(sssp[0]);
        
        Matcher matcher14 = pattern14.matcher(sssp[0]);

        boolean matchFound = matcher.find();

        boolean matchFound1 = matcher1.find();
        
        boolean matchFound2 = matcher2.find();
        
        boolean matchFound3 = matcher3.find();
        
        boolean matchFound10 = matcher11.find();
        
        boolean matchFound11 = matcher12.find();
        
        boolean matchFound12 = matcher13.find();
        
        boolean matchFound13 = matcher14.find();
        
        String hello = sssp[1];
        
        String[] whoop = hello.split("\\.", 2);
        
        if (whoop.length != 2) {
            
        System.out.println();

        System.out.println("This is not a correct email address."); 

        System.out.println();   
            
        recursion();
        
        } else {
            
            String regex4 = "[^a-z]";
            
            String regex5 = "^$";
            
            String regex6 = "\s";
            
            Pattern pattern4 = Pattern.compile(regex4, Pattern.CASE_INSENSITIVE);

            Pattern pattern5 = Pattern.compile(regex5, Pattern.CASE_INSENSITIVE);
            
            Pattern pattern6 = Pattern.compile(regex6, Pattern.CASE_INSENSITIVE);
            
            Matcher matcher4 = pattern4.matcher(whoop[0]);

            Matcher matcher5 = pattern5.matcher(whoop[0]);
            
            Matcher matcher6 = pattern6.matcher(whoop[0]);
            
            boolean matchFound4 = matcher4.find();

            boolean matchFound5 = matcher5.find();
            
            boolean matchFound6 = matcher6.find();
            
            Pattern pattern7 = Pattern.compile(regex4, Pattern.CASE_INSENSITIVE);

            Pattern pattern8 = Pattern.compile(regex5, Pattern.CASE_INSENSITIVE);
            
            Pattern pattern9 = Pattern.compile(regex6, Pattern.CASE_INSENSITIVE);
            
            Matcher matcher7 = pattern7.matcher(whoop[1]);

            Matcher matcher8 = pattern8.matcher(whoop[1]);
            
            Matcher matcher9 = pattern9.matcher(whoop[1]);
            
            boolean matchFound7 = matcher7.find();

            boolean matchFound8 = matcher8.find();
            
            boolean matchFound9 = matcher9.find();
            
            System.out.println();

            if(matchFound || matchFound1 || matchFound2 || matchFound3 || matchFound4 || matchFound5 || matchFound6 || matchFound7 || matchFound8 || matchFound9 || matchFound10 || matchFound11 || matchFound12 || matchFound13) {

                System.out.println("This is not a correct email address.");

                System.out.println();

                recursion();

            } else {

                System.out.println("This is a correct email address.");

                System.out.println();

                System.out.println("Do you still want to play? (say yes to play) ");

                System.out.println();

                Scanner scanner1 = new Scanner(System.in);

                String snow1 = scanner1.nextLine();

                if (snow1.equalsIgnoreCase("yes")) {

                    System.out.println();

                    recursion();

                } else {

                    System.out.println();  

                    System.out.println("Okay, then.");  
                    
                    scanner.close();
                    
                    scanner1.close();
                    
                }

            }   
            
        }
        
    }

}

}
于 2021-05-26T16:01:25.257 回答