用Java验证电子邮件 [英] Verify email in Java

查看:118
本文介绍了用Java验证电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Java代码中验证电子邮件地址是否有效。有效的,我不只是说它是正确的格式(someone@domain.subdomain),但这是一个真正活跃的电子邮件地址。



我几乎可以肯定,没有100%可靠的方式来做到这一点,因为这样的技术将是垃圾邮件发送者的梦想的东西。但是也许有一些技术给出了一个关于地址是否真实的一些有用的指示。

解决方案

周围要检查地址是否是有效的格式,这里是一个正则表达式,它验证它几乎是rfc2822(它没有捕获一些怪异的角落)。我在去年的网上找到了。

  private static final Pattern rfc2822 = Pattern.compile(
^ [A-Z0-9#$%&安培;! '!?* + / = ^ _`{|}〜 - ] +(?:\\ [A-Z0-9#$%&放大器;' * ?+ / = ^ _`{|}〜 - ] +)* @(?:?[A-Z0-9](在[a-Z0-9 - ] * [A-Z0-9])\ \。)+ [a-z0-9](?:[a-z0-9 - ] * [a-z0-9])?$
);

if(!rfc2822.matcher(email).matches()){
throw new Exception(Invalid address);
}

这将会处理简单的语法(大部分)。我知道的其他检查将让您检查域是否具有MX记录。它看起来像这样:

  Hashtable< String,String> env = new Hashtable< String,String>(); 

env.put(java.naming.factory.initial,com.sun.jndi.dns.DnsContextFactory);

DirContext ictx = new InitialDirContext(env);

属性attrs = ictx.getAttributes(domainName,new String [] {MX});

属性attr = attrs.get(MX);

if(attr == null)
//否MX记录
else
//如果attr.size()> 0,有一个MX记录

这个,我也在网上找到。来自此链接



如果这两个都通过,你很有可能拥有一个有效的地址。至于如果地址是自己的(不只是域名),那么它不是全部的,那么你真的不能检查。



请注意,第二个检查是时间密集型。它可以从毫秒到> 30秒的任何时间(如果DNS不响应和超时)。



希望这有帮助。



编辑



我想指出,至少代替正则表达式,有更好的方法来检查基本的有效性。 Don和Michael指出Apache Commons有一些东西,最近我发现你可以在InternetAddress上使用.validate()来检查地址是否真的是RFC-8222,这肯定比我的正则表达式更准确。 >

Is there any way to verify in Java code that an e-mail address is valid. By valid, I don't just mean that it's in the correct format (someone@domain.subdomain), but that's it's a real active e-mail address.

I'm almost certain that there's no 100% reliable way to do this, because such a technique would be the stuff of spammer's dreams. But perhaps there's some technique that gives some useful indication about whether an address is 'real' or not.

解决方案

Here is what I have around. To check that the address is a valid format, here is a regex that verifies that it's nearly rfc2822 (it doesn't catch some weird corner cases). I found it on the 'net last year.

private static final Pattern rfc2822 = Pattern.compile(
        "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"
);

if (!rfc2822.matcher(email).matches()) {
    throw new Exception("Invalid address");
}

That will take care of simple syntax (for the most part). The other check I know of will let you check if the domain has an MX record. It looks like this:

Hashtable<String, String> env = new Hashtable<String, String>();

env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");

DirContext ictx = new InitialDirContext(env);

Attributes attrs = ictx.getAttributes(domainName, new String[] {"MX"});

Attribute attr = attrs.get("MX");

if (attr == null)
    // No MX record
else
    // If attr.size() > 0, there is an MX record

This, I also found on the 'net. It came from this link.

If these both pass, you have a good chance at having a valid address. As for if the address it's self (not just the domain), it's not full, etc... you really can't check that.

Note that the second check is time intensive. It can take anywhere from milliseconds to >30 seconds (if the DNS does not respond and times out). It's not something to try and run real-time for large numbers of people.

Hope this helps.

EDIT

I'd like to point out that, at least instead of the regex, there are better ways to check basic validity. Don and Michael point out that Apache Commons has something, and I recently found out you can use .validate() on InternetAddress to have Java check that the address is really RFC-8222, which is certainly more accurate than my regex.

这篇关于用Java验证电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆