How to encode Internet address

前端 未结 2 464
无人及你
无人及你 2021-01-12 04:34

code to send email is following:

    MimeMessage msg = new MimeMessage(session);
    msg.setSubject(\"subject\", \"UTF-8\"); // here you specify your subjec         


        
2条回答
  •  离开以前
    2021-01-12 05:17

    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.

提交回复
热议问题