在java中验证电子邮件地址

时间:2012-04-07 05:38:26

标签: java spring-mvc

我究竟如何验证电子邮件地址的域名部分?我是否需要首先列出我的java类中的现有域,或者java InternetAddress.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这样的无效电子邮件显示有效..我究竟需要做什么..帮帮我..这个功能对于那些使用过它并经过测试的人来说也很好吗?

5 个答案:

答案 0 :(得分:3)

我认为你是从错误的角度来解决这个问题。从您的应用程序的角度来看,如果可以接收邮件,则应将电子邮件视为有效(更好,更有用)。这就是为什么所有这些论坛都不断向您发送激活电子邮件:)您应该向每个新的电子邮件地址发送一些随机字符串并将其保持在隔离状态,直到用户可以证明他已阅读该秘密为止。

这是因为域可能存在,甚至该域中的MX记录都可以存在于DNS中,但这些条件都不能保证地址有效 - 当你验证你真正声称它可以在以后的代码中用于某种目的,并且电子邮件地址的目的是接收邮件

答案 1 :(得分:1)

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

有关更多信息,另请参阅this page

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我认为没有完全有效的方式来刺激它。我们所能做的就是给这个模式带来烦恼,或者你更喜欢邮件领域,比如hhhgggmail.com。但你怎么能为“san@hhhgggmail.com确实存在”而烦恼呢?

SMTP do有一个命令'VEFY',但出于安全原因,几乎所有smtp服务器都没有实现此命令。

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

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;
}

答案 4 :(得分: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();
                    
                }

            }   
            
        }
        
    }

}

}