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


当前回答

我在这个地址上维护了一个类: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依赖项,但是如果您不想要它提供的次要功能,那么很容易删除它。

其他回答

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

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

当前的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);

使用官方的java电子邮件包是最简单的:

public static boolean isValidEmailAddress(String email) {
   boolean result = true;
   try {
      InternetAddress emailAddr = new InternetAddress(email);
      emailAddr.validate();
   } catch (AddressException ex) {
      result = false;
   }
   return result;
}
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));
    }
}