问题描述
我使用Javamail(javax.mail)发送邮件。我成功地调整我的邮件内容为utf-8。但我无法将主题行设置为utf-8编码的字符串。
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.
我尝试了
mail.setSubject(new String(subject.getBytes("utf-8"), "utf-8"));
,但它仍然作为Cp1252发送。来自邮件的示例标题如下:
on subject however it still sends as Cp1252. Example headers from mail are given below:
任何想法?
推荐答案
=http://java.sun.com/products/javamail/javadocs/javax/mail/internet/MimeMessage.html#setSubject%28java.lang.String,%20java.lang.String%29 =nofollow noreferrer > setSubject(String subject,String charset),这是一个方便的功能。
You should use setSubject(String subject, String charset) which is a convenient function for this purpose.
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]);
在它说:
从我的角度来看, Message.setSubject
应该是此目的的入口点。
From my point of view, Message.setSubject
should be the entry point for this purpose.
主题编码中的 cp1252
显示出来,因为它是您平台上的标准编码。
The cp1252
in your subject encoding shows up, because it is your standard encoding on your platform.
您发布的示例是
mail.setSubject(MimeUtility.encodeText(subject, "cp1252", "Q"));`
这篇关于Javamail改变主题行的字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!