Javamail 更改主题行的字符集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3451256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javamail changing charset of subject line
提问by AhmetB - Google
I am using Javamail (javax.mail) to send mails. I successfully adjusted contents of my mail as utf-8. However I could not set subject line as a utf-8 encoded string.
我正在使用 Javamail (javax.mail) 发送邮件。我成功地将邮件内容调整为 utf-8。但是我无法将主题行设置为 utf-8 编码字符串。
I tried even
我什至试过
mail.setSubject(new String(subject.getBytes("utf-8"), "utf-8"));
on subject however it still sends as Cp1252. Example headers from mail are given below:
然而,它仍然以 Cp1252 的形式发送。下面给出了来自邮件的示例标题:
Any ideas?
有任何想法吗?
example from mail headers http://m.friendfeed-media.com/a328a80db12f3c17a8aed06be106045354355abf
邮件标题中的示例 http://m.friendfeed-media.com/a328a80db12f3c17a8aed06be106045354355abf
采纳答案by Michael Konietzka
You should use setSubject(String subject, String charset)which is a convenient function for this purpose.
您应该使用setSubject(String subject, String charset)这是一个方便的函数。
Session session=Session.getDefaultInstance(new Properties());
MimeMessage mimeMsg= new MimeMessage(session);
String subject="Herr Müller reist nach \u0141\u00f3d\u017a.";
mimeMsg.setSubject(subject,"utf-8");
System.out.println(subject);
System.out.println(mimeMsg.getHeader("Subject")[0]);
In MimeUtilityit is said:
在MimeUtility 中是这样说的:
There are a set of methods to encode and decode MIME headers as per RFC 2047. Note that, in general, these methods are not needed when using methods such as setSubject and setRecipients; JavaMail will automatically encode and decode data when using these "higher level" methods. The methods below are only needed when maniuplating raw MIME headers using setHeader and getHeader methods.
根据 RFC 2047,有一组方法可以对 MIME 标头进行编码和解码。请注意,一般而言,使用 setSubject 和 setRecipients 等方法时不需要这些方法;当使用这些“更高级别”的方法时,JavaMail 将自动编码和解码数据。以下方法仅在使用 setHeader 和 getHeader 方法操作原始 MIME 标头时才需要。
From my point of view, Message.setSubject
should be the entry point for this purpose.
在我看来,Message.setSubject
应该是这个目的的切入点。
The cp1252
in your subject encoding shows up, because it is your standard encoding on your platform.
出现cp1252
在您的主题编码中,因为它是您平台上的标准编码。
Your posted example is the "result" of
您发布的示例是“结果”
mail.setSubject(MimeUtility.encodeText(subject, "cp1252", "Q"));`
回答by AhmetB - Google
Solved.
解决了。
mail.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
solves it and sends utf-8 encoded mail subjects. \n/
解决它并发送 utf-8 编码的邮件主题。\n/
the legal values for "encoding" are "Q" and "B"... The "Q" encoding is recommended for use when most of the characters to be encoded are in the ASCII character set; otherwise, the "B" encoding should be used.
"encoding" 的合法值为 "Q" 和 "B"... 当要编码的大多数字符为 ASCII 字符集时,建议使用 "Q" 编码;否则,应使用“B”编码。
回答by Sami
Problem solved!
问题解决了!
mail.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
solves it and sends utf-8 encoded mail subjects.
解决它并发送 utf-8 编码的邮件主题。
Why there is that "B" why there isn't ISO-something?
为什么有那个“B” 为什么没有 ISO 的东西?
回答by user2200192
I ran into a similar problem with Apache Camel Mail, which uses Java Mail. Setting
我在使用 Java Mail 的 Apache Camel Mail 中遇到了类似的问题。环境
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
before routing to SMTP, solved the problem.
在路由到 SMTP 之前,解决了问题。
回答by Mna
The MimeMessage.setSubject(String subject, String charset)method will solve the problem: mimeMsg.setSubject(subject,"utf-8");
所述MimeMessage.setSubject(字符串对象,字符串charset)方法将解决这个问题:mimeMsg.setSubject(主题, “UTF-8”);
This is an updated link. Previous documentation link was dismissed by Oracle after Oracle bought the Sun.
这是一个更新的链接。在 Oracle 收购 Sun 后,Oracle 取消了之前的文档链接。
回答by seunggabi
Solved.
解决了。
import static java.nio.charset.StandardCharsets.*;
byte[] bytes = subject.getBytes(ISO_8859_1);
subject = new String(bytes, UTF_8);
mail.setSubject(subject);