Allan
Allan

Reputation: 143

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

Domains with special danish characters such as æ ø å are now allowed, but I can't 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 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.

Upvotes: 14

Views: 28630

Answers (5)

Harish Ladi
Harish Ladi

Reputation: 1

In My case, I missed the Comma separator in between the mails list.

message.addRecipients(Message.RecipientType.CC,
                    InternetAddress.parse(emailConfigs.getProperty(ReportConstants.CC_MAIL_IDS)));

In the properties file: cc: [email protected], [email protected] [email protected]

If you observe in between mail2 and mail3 I missed the comma separator. I just added that and it is resolved. More of this issue will come when we miss some standard format.

Upvotes: 0

Dungeon Hunter
Dungeon Hunter

Reputation: 20593

Java Mail 1.6 supports Internationalized Email Addresses.

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

-Dmail.mime.allowutf8=true 

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

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

The java source encoding was UTF-8. The operating system was Linux. Or the cause might be that you are using a 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

Upvotes: 1

felix
felix

Reputation: 61

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.

To encode only the domain part with the java.net.IDN class, i use the following 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);
    }

}

Upvotes: 4

Aaron Digulla
Aaron Digulla

Reputation: 328556

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

Upvotes: 2

Related Questions