Java - 使用 InternetAddress 验证电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12862747/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:32:31  来源:igfitidea点击:

Java - validate email with InternetAddress

javavalidationemail

提问by Jerome Ansia

I got this method:

我得到了这个方法:

public static boolean isValidEmailAddress(String email) {
       boolean result = true;
       try {
          InternetAddress emailAddr = new InternetAddress(email);
          emailAddr.validate();
       } catch (AddressException ex) {
          result = false;
       }
       return result;
}

but that give me this exception:

但这给了我这个例外:

java.lang.NullPointerException at javax.mail.internet.AddressParser.tokenizeAddress(AddressParser.java:645) at javax.mail.internet.AddressParser.parseAddress(AddressParser.java:113) at javax.mail.internet.InternetAddress.(InternetAddress.java:70) at javax.mail.internet.InternetAddress.(InternetAddress.java:61) at controller.CtrlUser.isValidEmailAddress(CtrlUser.java:166) at controller.CtrlSeller.register(CtrlSeller.java:41) at testController.CtrlSellerUnitTestCase.testRegister(CtrlSellerUnitTestCase.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) ....

java.lang.NullPointerException at javax.mail.internet.AddressParser.tokenizeAddress(AddressParser.java:645) at javax.mail.internet.AddressParser.parseAddress(AddressParser.java:113) at javax.mail.internet.InternetAddress.(InternetAddress) .java:70) at javax.mail.internet.InternetAddress.(InternetAddress.java:61) at controller.CtrlUser.isValidEmailAddress(CtrlUser.java:166) at controller.CtrlSeller.register(CtrlSeller.java:41) at testController。 CtrlSellerUnitTestCase.testRegister(CtrlSellerUnitTestCase.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImplatingMethod:AccessorImplatingMethod:Invoke0(Native Method) 25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners。 model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores. java:28) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 在 org.junit.runners.BlockJUnit4ClassRunner .runChild(BlockJUnit4ClassRunner.java:68) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) ....

回答by Tom G

It works for me- check to see if you are actually passing a valid string. It appears you are sending null as your email parameter

它对我有用 - 检查你是否真的传递了一个有效的字符串。看来您正在发送 null 作为您的电子邮件参数

回答by Dungeon Hunter

make sure that the email parameter you are passing is not null

确保您传递的电子邮件参数不为空

回答by fdurmus77

Try following code:

尝试以下代码:

java.net.IDN.toASCII    

public static boolean isValidEmail(String email) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(java.net.IDN.toASCII(email));
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    System.out.println("==> "+result+" : "+email);
    return result;
}