java JavaMail:“域包含字符串中的控制或空格”错误消息,因为域包含丹麦语字符

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

JavaMail: "Domain contains control or whitespace in string" errormessage because of domain with Danish characters

javaemaildnsjavamail

提问by Allan

Domains with special danish characters such as ? ? ? are now allowed, but I can't force java mail to accept this.

带有特殊丹麦字符的域,例如 ? ? ? 现在允许,但我不能强制 java 邮件接受这个。

    @Test()
public void testMailAddressWithDanishCharacters1() throws AddressException, UnsupportedEncodingException {
    InternetAddress cAddress = new InternetAddress( "test@test?xample12345123.com", null, "utf-8" );
    System.out.println( cAddress.toString() );
    cAddress.validate();
}

@Test()
public void testMailAddressWithDanishCharacters2() throws AddressException, UnsupportedEncodingException {
    InternetAddress cAddress = new InternetAddress( "test@test?xample12345123.com", false );
    System.out.println( cAddress.toString() );
    cAddress.validate();
}

@Test()
public void testMailAddressWithDanishCharacters3() throws AddressException, UnsupportedEncodingException {
    InternetAddress cAddress = new InternetAddress( "test@test?xample12345123.com", true );
    System.out.println( cAddress.toString() );
    cAddress.validate();
}

All of the tests fail in either the constructor of InternetAddress or in the validate() method. How can I handle these special danish characters in the domain. I bet that other countries have the same issue with their domains vs emails in javamail InternetAddress.

所有测试都在 InternetAddress 的构造函数或 validate() 方法中失败。如何处理域中的这些特殊丹麦字符。我敢打赌,其他国家/地区的域名与 javamail InternetAddress 中的电子邮件存在相同的问题。

采纳答案by Aaron Digulla

Java Mail doesn't support i18n domain names, so you must use the standard rules to escape them using the IDNA rules.

Java Mail 不支持 i18n 域名,因此您必须使用标准规则使用IDNA 规则对它们进行转义。

回答by felix

Currently mail servers generally don't accept non-ASCII characters in the local part, only the domain part (following the '@' sign) is supported with IDN.

目前邮件服务器一般不接受本地部分的非 ASCII 字符,IDN 仅支持域部分(在“@”符号之后)。

To encode only the domain part with the java.net.IDN class, i use the following Util.

要仅使用 java.net.IDN 类对域部分进行编码,我使用以下 Util。

(Code not tested in production, but it should work)

(代码未在生产中测试,但它应该可以工作)

import java.net.IDN;


public class IDNMailHelper {

    public static String toIdnAddress(String mail) {
        if (mail == null) {
            return null;
        }
        int idx = mail.indexOf('@');
        if (idx < 0) {
            return mail;
        }
        return localPart(mail, idx) + "@" + IDN.toASCII(domain(mail, idx));
    }

    private static String localPart(String mail, int idx) {
        return mail.substring(0, idx);
    }

    private static String domain(String mail, int idx) {
        return mail.substring(idx + 1);
    }

}

回答by Joop Eggen

I did run it with Java 7, javax.mail 1.4 (from Maven repository). And there it worked.

我确实使用 Java 7、javax.mail 1.4(来自 Maven 存储库)运行它。在那里它起作用了

The java source encoding was UTF-8. The operating system was Linux. Or the cause might be that you are using a jee jar.

Java 源代码编码为 UTF-8。操作系统是Linux。或者原因可能是您使用的是 jee jar。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jeggen.test2.AppTest
test@test?xample12345123.com
test@test?xample12345123.com
test@test?xample12345123.com
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

回答by Dungeon Hunter

Java Mail 1.6 supports Internationalized Email Addresses.

Java Mail 1.6 支持国际化的电子邮件地址。

https://java.net/projects/javamail/forums/forum/topics/81613-Does-JavaMail-support-Internationalized-Domain-Names-IDN-

https://java.net/projects/javamail/forums/forum/topics/81613-Does-JavaMail-support-Internationalized-Domain-Names-IDN-

It's still in development you can try out with the snapshot release. Also add the JVM argument

它仍在开发中,您可以尝试使用快照版本。还要添加 JVM 参数

-Dmail.mime.allowutf8=true