在 Java 中验证电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/153716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Verify email in Java
提问by Dónal
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 ([email protected]), but that's it's a real active e-mail address.
有什么方法可以在 Java 代码中验证电子邮件地址是否有效。有效,我不仅仅是指它的格式正确([email protected]),而是指它是一个真正有效的电子邮件地址。
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.
我几乎可以肯定没有 100% 可靠的方法可以做到这一点,因为这样的技术将是垃圾邮件发送者的梦想。但也许有一些技术可以提供一些关于地址是否“真实”的有用指示。
回答by Joe Skora
Without sending an email, it could be hard to get 100%, but if you do a DNS lookup on the hostthat should at least tell you that it is a viable destination system.
不发送电子邮件,可能很难获得 100%,但如果您在主机上进行 DNS 查找,至少应该告诉您这是一个可行的目标系统。
回答by Lasar
Do a DNS lookup on the hostname to see if that exists. You could theoretically also initiate a connection to the mailserver and see if it tells you whether the recipient exists, but I think many servers pretend they know an address, then reject the email anyway.
对主机名进行 DNS 查找以查看是否存在。理论上,您也可以启动与邮件服务器的连接,看看它是否告诉您收件人是否存在,但我认为许多服务器假装他们知道一个地址,然后无论如何都会拒绝电子邮件。
回答by MBCook
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.
这是我周围的东西。要检查地址是否是有效格式,这里有一个正则表达式,可以验证它是否接近 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");
}
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:
这将处理简单的语法(在大多数情况下)。我知道的另一项检查将让您检查域是否有 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);
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.
请注意,第二次检查是时间密集型的。它可能需要从几毫秒到 >30 秒的任何时间(如果 DNS 没有响应并超时)。这不是尝试为大量人实时运行的东西。
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.
我想指出的是,至少除了正则表达式之外,还有更好的方法来检查基本有效性。Don 和 Michael 指出 Apache Commons 有一些东西,我最近发现你可以在 InternetAddress 上使用 .validate() 来让 Java 检查地址是否真的是 RFC-8222,这肯定比我的正则表达式更准确。
回答by WMR
You cannot really verify that an email exists, see my answer to a very similar question here: Email SMTP validator
您无法真正验证电子邮件是否存在,请在此处查看我对一个非常相似的问题的回答:电子邮件 SMTP 验证器
回答by Ashkan Aryan
Apache commons provides an email validator classtoo, which you can use. Simply pass your email address as an argument to isValid method.
Apache commons 也提供了一个电子邮件验证器类,您可以使用它。只需将您的电子邮件地址作为参数传递给 isValid 方法。
回答by Aquarelle
I'm not 100% sure, but isn't it possible to send an RCPTSMTP command to a mail server to determine if the recipient is valid? It would be even more expensive than the suggestion above to check for a valid MX host, but it would also be the most accurate.
我不是 100% 确定,但是否可以将RCPTSMTP 命令发送到邮件服务器以确定收件人是否有效?检查有效的 MX 主机会比上面的建议更昂贵,但它也是最准确的。
回答by Craigo
If you're using GWT, you can't use InternetAddress, and the pattern supplied by MBCook is pretty scary.
如果您使用 GWT,则不能使用 InternetAddress,而且 MBCook 提供的模式非常可怕。
Here is a less scary regex (might not be as accurate):
这是一个不那么可怕的正则表达式(可能不那么准确):
public static boolean isValidEmail(String emailAddress) {
return emailAddress.contains(" ") == false && emailAddress.matches(".+@.+\.[a-z]+");
}
回答by Thorbj?rn Ravn Andersen
The only way you can be certain is by actually sending a mail and have it read.
您可以确定的唯一方法是实际发送邮件并阅读。
Let your registration process have a step that requires responding to information found only in the email. This is what others do.
让您的注册过程有一个步骤,要求回复仅在电子邮件中找到的信息。这就是其他人所做的。
回答by asa
public static boolean isValidEmail(String emailAddress) {
return emailAddress.contains(" ") == false && emailAddress.matches(".+@.+\.[a-z]+");
}