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


当前回答

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

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

测试用例:

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

其他回答

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

如果您正在尝试从客户端接收表单验证,或者仅仅是bean验证,请保持简单。 最好是做一个宽松的电子邮件验证,而不是做一个严格的电子邮件验证并拒绝一些人(例如,当他们试图注册你的web服务时)。 由于电子邮件的用户名部分几乎允许任何内容,而且每个月都有这么多新域名被添加(例如。company, . enterprise, .estate),所以不限制是更安全的:

Pattern pattern = Pattern.compile("^.+@.+\\..+$");
Matcher matcher = pattern.matcher(email);

下面是我的实用方法,我只需要使用RFC中允许的字符合理地区分blah@domain地址。地址必须事先转换为小写。

public class EmailAddressValidator {

    private static final String domainChars = "a-z0-9\\-";
    private static final String atomChars = "a-z0-9\\Q!#$%&'*+-/=?^_`{|}~\\E";
    private static final String emailRegex = "^" + dot(atomChars) + "@" + dot(domainChars) + "$";
    private static final Pattern emailPattern = Pattern.compile(emailRegex);

    private static String dot(String chars) {
        return "[" + chars + "]+(?:\\.[" + chars + "]+)*";
    }

    public static boolean isValidEmailAddress(String address) {
        return address != null && emailPattern.matcher(address).matches();
    }

}

我在这个地址上维护了一个类:http://lacinato.com/cm/software/emailrelated/emailaddress

它基于Les Hazlewood的类,但有许多改进并修复了一些错误。Apache许可证。

I believe it is the most capable email parser in Java, and I have yet to see one more capable in any language, though there may be one out there. It's not a lexer-style parser, but uses some complicated java regex, and thus is not as efficient as it could be, but my company has parsed well over 10 billion real-world addresses with it: it's certainly usable in a high-performance situation. Maybe once a year it'll hit an address that causes a regex stack overflow (appropriately), but these are spam addresses which are hundreds or thousands of characters long with many many quotes and parenthesis and the like.

RFC 2822和相关规范在电子邮件地址方面是非常宽松的,所以这样的类对于大多数用途来说是多余的。例如,以下是一个合法的地址,根据规范,空格和所有:

"<bob \" (here) " < (hi there) "bob(the man)smith" (hi) @ (there) example.com (hello) > (again)

没有邮件服务器允许这样做,但是这个类可以解析它(并将其重写为可用的形式)。

我们发现现有的Java电子邮件解析器选项不够持久(也就是说,它们都不能解析一些有效地址),因此创建了这个类。

该代码有良好的文档记录,并且有许多易于更改的选项来允许或禁止某些电子邮件表单。它还提供了许多方法来访问地址的某些部分(左侧、右侧、个人姓名、注释等),解析/验证邮箱列表头,解析/验证返回路径(在头中是唯一的),等等。

编写的代码有一个javamail依赖项,但是如果您不想要它提供的次要功能,那么很容易删除它。

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

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

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