Java中有哪些好的电子邮件地址验证库?有任何替代公共验证器的方法吗?


当前回答

public class Validations {

    private Pattern regexPattern;
    private Matcher regMatcher;

    public String validateEmailAddress(String emailAddress) {

        regexPattern = Pattern.compile("^[(a-zA-Z-0-9-\\_\\+\\.)]+@[(a-z-A-z)]+\\.[(a-zA-z)]{2,3}$");
        regMatcher   = regexPattern.matcher(emailAddress);
        if(regMatcher.matches()) {
            return "Valid Email Address";
        } else {
            return "Invalid Email Address";
        }
    }

    public String validateMobileNumber(String mobileNumber) {
        regexPattern = Pattern.compile("^\\+[0-9]{2,3}+-[0-9]{10}$");
        regMatcher   = regexPattern.matcher(mobileNumber);
        if(regMatcher.matches()) {
            return "Valid Mobile Number";
        } else {
            return "Invalid Mobile Number";
        }
    }

    public static void main(String[] args) {

        String emailAddress = "suryaprakash.pisay@gmail.com";
        String mobileNumber = "+91-9986571622";
        Validations validations = new Validations();
        System.out.println(validations.validateEmailAddress(emailAddress));
        System.out.println(validations.validateMobileNumber(mobileNumber));
    }
}

其他回答

您想验证什么?电子邮件地址?

只能检查电子邮件地址的格式是否符合要求。参见标准:RFC2822。最好的方法是使用正则表达式。如果不发一封邮件,你永远不会知道是否真的存在。

我检查了公共验证器。它包含一个org.apache.commons.validator.EmailValidator类。看起来是个不错的起点。

当前的Apache Commons Validator版本为1.3.1。

进行验证的类是org.apache.commons.validator.EmailValidator。它导入了org.apache.oro.text.perl。Perl5Util来自一个退役的雅加达ORO项目。

BTW, I found that there is a 1.4 version, here are the API docs. On the site it says: "Last Published: 05 March 2008 | Version: 1.4-SNAPSHOT", but that's not final. Only way to build yourself (but this is a snapshot, not RELEASE) and use, or download from here. This means 1.4 has not been made final for three years (2008-2011). This is not in Apache's style. I'm looking for a better option, but didn't find one that is very adopted. I want to use something that is well tested, don't want to hit any bugs.

可以像其他答案中提到的那样使用Apache Commons验证器。

pom.xml:

<dependency>
    <groupId>commons-validator</groupId>
    <artifactId>commons-validator</artifactId>
    <version>1.4.1</version>
</dependency>

build.gradle:

compile 'commons-validator:commons-validator:1.4.1'

导入:

import org.apache.commons.validator.routines.EmailValidator;

代码:

String email = "myName@example.com";
boolean valid = EmailValidator.getInstance().isValid(email);

并允许本地地址

boolean allowLocal = true;
boolean valid = EmailValidator.getInstance(allowLocal).isValid(email);

尽管有许多替代Apache commons的方法,但它们的实现充其量只是基本的(就像Apache commons的实现本身一样),在其他情况下甚至是完全错误的。

我也会远离所谓的简单的“非限制性”正则表达式;没有这样的事。例如,@根据上下文被允许多次使用,你怎么知道需要的那个在那里?简单的正则表达式无法理解它,即使电子邮件应该是有效的。任何更复杂的东西都很容易出错,甚至包含隐藏的性能杀手。你要怎么维护这样的东西?

我所知道的唯一一个全面的RFC兼容的基于regex的验证器是email-rfc2822-validator,它的“精炼”regex适当地命名为Dragons.java。它只支持较旧的RFC-2822规范,尽管它足够满足现代需求(RFC-5322在日常使用范围之外的地方更新了它)。

但真正需要的是一个词法分析器,它可以正确地解析字符串,并根据RFC语法将其分解为组件结构。EmailValidator4J在这方面似乎很有前途,但仍然很年轻,而且有局限性。

另一种选择是使用webservice,比如Mailgun经过实战测试的验证webservice或Mailboxlayer API(只使用第一个谷歌结果)。它不是严格的RFC兼容,但是对于现代需求来说已经足够好了。

回答迟了,但我觉得简单而有价值:

    public boolean isValidEmailAddress(String email) {
           String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
           java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
           java.util.regex.Matcher m = p.matcher(email);
           return m.matches();
    }

测试用例:

出于生产目的,域名验证应该在网络上执行。