如何编码互联网地址
发送电子邮件的代码如下:
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);
我的probelem是,因为我已经编码主题在utf-8我如何编码收件人地址ie 。 新的InternetAddress(地址)
My probelem is that as as i have encoded subject in utf-8 how can i encode recipient address ie. new InternetAddress(address)
电子邮件地址应遵循RFC822标准
JavaMail的MimeMessage使用 InternetAddress :
此类表示使用语法为
RFC822的Internet电子邮件地址。典型的地址语法格式为user@host.domain或
个人名称< user@host.domain>。
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格式说:
请注意,RFC 822将字符集全部限制为 ASCII 。在
的实践中,其他字符(例如ä或é)通常在用于评论目的(和注释)的引号
字符串中工作,但它们不能在正确的地址中使用
。 p>
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.
地址的个人名称支持不同的字符集
InternetAddress使用个人名称: / p>
Personal names for address supports different charsets
InternetAddress uses a personal name:
如果名称包含非US-ASCII字符,
,那么该名称将使用指定的字符集进行编码,按照RFC
2047.如果名称仅包含US-ASCII字符,则不会编码,并且名称也被使用。
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.
设置编码的字符集,有一个 InternetAddress#constructor 一>。查看来源:
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);
}
它只是调用 setPersonal(..),因此选择最适合您的方式。
it just calls setPersonal(..), thus choose the way which is the most convenient for you.
要查找字符集,请使用 Charset.forName()。
To lookup a charset, use Charset.forName().