JavaMail: "Domain contains control or space in string" errormessage due to domain with Danish charaters

Domains with special Danish characters, such as æ ø å, are now allowed, but I cannot force java mail to accept this.

@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 tests do not work either in the InternetAddress constructor or in the validate () method. How can I handle these special Danish characters in the domain. I bet that in other countries the same problem is related to their domains and email in javamail InternetAddress.

+4
source share
4 answers

Java Mail does not support i18n domain names, so you should use standard rules to avoid them using IDNA rules .

+1
source

Currently, mail servers usually do not accept non-ASCII characters in the local part, only the part of the domain (after the "@" sign) is supported with IDN.

To encode only part of a domain using the java.net.IDN class, I use the following Util.

(The code is 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); } } 
+2
source

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

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

 ------------------------------------------------------- TESTS ------------------------------------------------------- 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 
+1
source

Java Mail 1.6 supports international email addresses.

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

It is still under development, you can try with the release of the snapshot. Also add a JVM argument

 -Dmail.mime.allowutf8=true 
0
source

Source: https://habr.com/ru/post/1345838/


All Articles