How to encode Internet address

核能气质少年 提交于 2019-12-19 06:03:03

问题


code to send email is following:

    MimeMessage msg = new MimeMessage(session);
    msg.setSubject("subject", "UTF-8"); // here you specify your subject encoding
    msg.setContent("yourBody", "text/plain; charset=utf-8");

    msg.setFrom("senderAddress");
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(address));
    Transport.send(msg);

My probelem is that as as i have encoded subject in utf-8 how can i encode recipient address ie. new InternetAddress(address)


回答1:


Email address should follow RFC822 standard

JavaMail's MimeMessage uses InternetAddress:

This class represents an Internet email address using the syntax of RFC822. Typical address syntax is of the form "user@host.domain" or "Personal Name < user@host.domain >".

RFC822 format says:

Note that RFC 822 limits the character repertoire to ASCII. In practice, other characters (such as ä or é) usually work inside quoted strings used for commenting purposes (and comments), but they must not be used in addresses proper.

Personal names for address supports different charsets

InternetAddress uses a personal name:

If the name contains non US-ASCII characters, then the name will be encoded using the specified charset as per RFC 2047. If the name contains only US-ASCII characters, no encoding is done and the name is used as is.

To set charset for encoding, there is a InternetAddress#constructor. Looking at sources:

public InternetAddress(String address, String personal, String charset)
        throws UnsupportedEncodingException {
    this.address = address;
    setPersonal(personal, charset);
}

it just calls setPersonal(..), thus choose the way which is the most convenient for you.

To lookup a charset, use Charset.forName().




回答2:


I do this, where addressString is an email address with NLS characters:

InternetAddress address = new InternetAddress(addressString);
String personal = address.getPersonal();
if(personal != null) {
  address.setPersonal(personal, "utf-8");
}

getPersonal() gets the raw personal name if there is one, because if you constructed the InternetAddress with a single string, or using InternetAddress.parse(), you won't have the personal name part in a separate string:

public java.lang.String getPersonal()

Get the personal name. If the name is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as is.

Then setPersonal() sets the string back again but this time telling InternetAddress to encode it:

public void setPersonal(java.lang.String name, java.lang.String charset)

Set the personal name. If the name contains non US-ASCII characters, then the name will be encoded using the specified charset as per RFC 2047. If the name contains only US-ASCII characters, no encoding is done and the name is used as is.



来源:https://stackoverflow.com/questions/10830770/how-to-encode-internet-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!