Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Send email to a Non-ASCII email address

I want to send an email to a Non-ASCII email address and I am not sure what is the recommended procedure using JDK8.

How should I deal with the following email addresses?

  1. [email protected]
  2. test@Sörensen.de
  3. Dörte@Sörensen.de

Are there any security considerations to be aware of?

Would this sample code be enough?

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);
    }

}
like image 888
Adrian Avatar asked Oct 19 '16 13:10

Adrian


3 Answers

Paweł is essentially right that the addresses should be encoded as UTF-8 if your server supports the SMTPUTF8 extension. Support for SMTPUTF8 is in the JavaMail 1.6 release.

To enable this support, you need to set the JavaMail Session property mail.mime.allowutf8 to true. Be sure to only set it when the mail server supports UTF-8.

For older versions of JavaMail, a possible workaround involves converting the Java Unicode String to a UTF-8 encoded byte array, then creating a Java Unicode String where each byte is a separate iso-8859-1 character. I haven't tried this so I don't know if it will run into other issues.

For example:

address = new String(address.getBytes("utf-8"), "iso-8859-1");
like image 73
Bill Shannon Avatar answered Nov 18 '22 02:11

Bill Shannon


Due to RFC6530 you should actually encode the email as UTF-8. This mean you should not use IDN here.
If your outgoing SMTP servers supports EAI (internationalized emails), that should not be a problem.

Please keep in mind that EAI may pose serious security risk. If you decide to handle in your application (service?) be sure to prevent users from registering similarly looking email addresses, i.e. the ones using different scripts. For instance, one of these guys should be rejected:

  • Dörte@Sörensen.de
  • Döгtе@Sörensen.de
like image 31
Paweł Dyda Avatar answered Nov 18 '22 03:11

Paweł Dyda


For the headers of your email, the solution to this is RFC-2047, which defines a specific encoding protocol for non-ISO-646 characters in email. Java Mail has a class for using this: MimeUtility. Punicode/IDNS is inappropriate for header usage.

As Bill Shannon, another answerer here, pointed out in this answer, Java Mail will, in general, apply MimeUtil to your headers automatically. As for a question in a comment about intervening mail servers, I'm only answering your question about the text in the headers, which nothing will bother. As for the SMTP protocol messages needed to actually get the message to the right place, I don't claim to know.

like image 1
bmargulies Avatar answered Nov 18 '22 01:11

bmargulies