Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javamail problem with ñ characters in mail addresses

I'm having problems with parse method when usising ñ character:

essage.setRecipients(Message.RecipientType.TO, internetAddress.parse("somedir.withñchar@m ailserver.com",false));

I'm passing false for the strict parameter, but always i get the error:

javax.mail.internet.AddressException: Local address contains control or whitespace in string ``somedir.with±[email protected]''
at Mailer.main(Mailer.java:386)
Caused by: javax.mail.internet.AddressException: Local address contains control
or whitespace in string ``somedir.with±[email protected]''
        at javax.mail.internet.InternetAddress.checkAddress(InternetAddress.java
:1155)
        at javax.mail.internet.InternetAddress.parse(InternetAddress.java:1044)
        at javax.mail.internet.InternetAddress.parse(InternetAddress.java:575)
        at Mailer.main(Mailer.java:377)
like image 217
Guille Avatar asked Jan 05 '11 19:01

Guille


2 Answers

If you are still with this problem, I advise you to add double quotes to your unusual e-mail address like I did. It worked for me, because checkAddress method of InternetAddress give up to validate quoted address. That's the easy and quick solution.

Example:

unusual_email_without_at_sign_or_with_accentuátion (won't work)
"unusual_email_without_at_sign_or_with_accentuátion" (it works!)

The perfect and correct solution would be Java Team to fix InternetAddress class' bug that even receiving "strict=false" at constructor is checking the e-mail syntax, when it shouldn't.

Example of the bug:

InternetAddress i = new InternetAddress("unusual_email_without_at_sign_or_with_accentuátion ", false)

Example of the workaround solution:

InternetAddress i = new InternetAddress("\"unusual_email_without_at_sign_or_with_accentuátion\"", false)

The approach of creating a new class that extends Address won't work, since Transport.send() for some reason seems to validate again the address and the exception is also launched.

Please, let us know if it helped!

like image 151
André Avatar answered Oct 06 '22 01:10

André


Verify that your ñ is actually an n-tilde. The following works perfectly for me

InternetAddress[] list = InternetAddress.parse("fred.joñ[email protected]", false);
for (InternetAddress a : list) {
    System.out.println("[" + a + "]");
}

It also works when replacing the ñ with a unicode escape (in the source .java file)

InternetAddress[] list = InternetAddress.parse("fred.jo\[email protected]", false);

You could try replacing it like this just as a test.

Edit: BTW I had also used parse("...") with the address and no second parameter, and passed true for strict, both of which also worked.

like image 26
Stephen P Avatar answered Oct 06 '22 01:10

Stephen P