簡體   English   中英

在 java 中驗證 email 地址

[英]Validating email address in java

我究竟如何驗證 email 地址的域部分? 我是否需要首先列出我的 java class 中的現有域,或者 java 的Inte.netAddress.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 有一個 email 地址並存儲在 regEmail 中。問題甚至是無效的電子郵件,如san@hhhgggmail.com它顯示有效..我到底需要做什么..幫助我..也是這個function 對那些使用過和測試過的人來說工作正常嗎?

我認為您是從錯誤的角度來解決問題的。 從您的應用程序的角度來看,如果 email 可以接收郵件,則它應該被視為有效(更好,有用)。 這就是為什么所有這些論壇不斷向您發送激活 email:) 您應該向每個新的 email 地址發送一些隨機字符串並將其保存在隔離區state 中,直到用戶可以證明他閱讀了秘密。

這是因為該域可能存在,甚至該域的 MX 記錄可能存在於 DNS 中,但這些條件都不能保證該地址有效- 同樣,當您驗證某些內容時,您實際上是在聲明它可以在以后使用出於某種目的在您的代碼中,email 地址的目的是接收郵件

我不知道 Java 是否有自動方式。但我會查找域的 MX 記錄。 如果存在 MX 記錄,域可能會收到郵件。

另請參閱此頁面以獲取更多信息。

為什么不在域部分使用.netAddres.getByName

我認為沒有完全有效的方法來驗證它。 我們所能做的就是驗證模式,或者更多地驗證郵件域,例如 hhhgggmail.com。 但是你怎么能確定“san@hhhgggmail.com 確實存在”呢?

SMTP 確實有一個命令“VEFY”,但出於安全原因,幾乎所有 smtp 服務器都沒有執行此命令。

哦,你想要vefy域。 所有 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;
}

請忽略變量名,因為它們不專業:

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

            }   
            
        }
        
    }

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM