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依赖项,但是如果您不想要它提供的次要功能,那么很容易删除它。
其他回答
下面是我的实用方法,我只需要使用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();
}
}
您想验证什么?电子邮件地址?
只能检查电子邮件地址的格式是否符合要求。参见标准:RFC2822。最好的方法是使用正则表达式。如果不发一封邮件,你永远不会知道是否真的存在。
我检查了公共验证器。它包含一个org.apache.commons.validator.EmailValidator类。看起来是个不错的起点。
回答迟了,但我觉得简单而有价值:
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();
}
测试用例:
出于生产目的,域名验证应该在网络上执行。
尽管有许多替代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 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
推荐文章
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 我如何清除一个linux邮箱与大量的电子邮件?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?
- 将Date对象转换为日历对象
- 在Java中保存最后N个元素的大小有限的队列