How to encode Internet address

前端 未结 2 466
无人及你
无人及你 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:10

    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().

提交回复
热议问题