Java中有哪些好的电子邮件地址验证库?有任何替代公共验证器的方法吗?
当前回答
似乎没有任何完美的库或方法可以自己做到这一点,除非您必须有时间向电子邮件地址发送电子邮件并等待回复(尽管这可能不是一个选项)。我最终使用了这里的一个建议http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/,并调整了代码,使其可以在Java中工作。
public static boolean isValidEmailAddress(String email) {
boolean stricterFilter = true;
String stricterFilterString = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
String laxString = ".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
String emailRegex = stricterFilter ? stricterFilterString : laxString;
java.util.regex.Pattern p = java.util.regex.Pattern.compile(emailRegex);
java.util.regex.Matcher m = p.matcher(email);
return m.matches();
}
其他回答
Apache Commons通常被认为是一个可靠的项目。但是请记住,如果你想确保它是一个真实的电子邮件,并且所有者希望它在你的网站上使用,你仍然需要发送一封验证邮件到这个地址。
编辑:有一个错误,它对域名的限制太大,导致它不接受来自新顶级域名的有效电子邮件。 此错误已于03/Jan/15 02:48在common -validator版本1.4.1中解决
当前的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.
这是最好的方法:
public static boolean isValidEmail(String enteredEmail){
String EMAIL_REGIX = "^[\\\\w!#$%&’*+/=?`{|}~^-]+(?:\\\\.[\\\\w!#$%&’*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\\\.)+[a-zA-Z]{2,6}$";
Pattern pattern = Pattern.compile(EMAIL_REGIX);
Matcher matcher = pattern.matcher(enteredEmail);
return ((!enteredEmail.isEmpty()) && (enteredEmail!=null) && (matcher.matches()));
}
来源: http://howtodoinjava.com/2014/11/11/java-regex-validate-email-address/
http://www.rfc-editor.org/rfc/rfc5322.txt
我在这个地址上维护了一个类: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依赖项,但是如果您不想要它提供的次要功能,那么很容易删除它。
可以像其他答案中提到的那样使用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);
推荐文章
- 指定的子节点已经有一个父节点。你必须先在子对象的父对象上调用removeView() (Android)
- 对于一个布尔字段,它的getter/setter的命名约定是什么?
- 如何获得当前屏幕方向?
- 如何在Android中渲染PDF文件
- 如何计算一个元素在列表中出现的次数
- c++中类似于java的instanceof
- 我如何解决错误“minCompileSdk(31)指定在一个依赖的AAR元数据”在本机Java或Kotlin?
- 如何POST表单数据与Spring RestTemplate?
- Mockito中检测到未完成的存根
- 我应该如何复制字符串在Java?
- “while(true)”循环有那么糟糕吗?
- 这个方法签名中的省略号(…)是干什么用的?
- Java:如何测试调用System.exit()的方法?
- 用于匹配英国邮政编码的正则表达式
- 带有返回类型的Java方法在没有返回语句的情况下编译