Java 邮件不支持电子邮件主题中的 UTF-8 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4406538/
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
Java Mail is not supporting UTF-8 Characters in Subject of the email
提问by Kalyan Raju
Here is my code of setting subject to the email:
这是我设置电子邮件主题的代码:
String bodyMessage="Dear Renav?il?le? Fran?ois? InCites™";
String subject = "Your new InCites™ subscription";
Properties _sessionProperties = new Properties();
_sessionProperties.put("mail.transport.protocol", "smtp");
_sessionProperties.put("mail.smtp.host", "hostname");
_sessionProperties.put("mail.smtp.port", "25");
Session session = Session.getInstance(_sessionProperties, null);
MimeMessage mimemsg = new MimeMessage(session);
mimemsg.addRecipients(Message.RecipientType.TO, "[email protected]");
mimemsg.setSubject(subject, "UTF-8");
// Create a multi-part message
MimeMultipart multipart = new MimeMultipart();
// Set the subType
multipart.setSubType("alternative");
BodyPart part = new MimeBodyPart();
part.setContent(bodyMessage, "charset=UTF-8");
// Set the emailBody and emailType to MIME BodyPart
part.setDataHandler(new DataHandler(new ByteArrayDataSource(
bodyMessage, "text/html;")));
// Add the MIME BodyPart to MIME multiPart
multipart.addBodyPart(part);
// Put parts in message
mimemsg.setContent(multipart);
// Send message
Transport.send(mimemsg);
But still in email subject it still shows as "Your new InCites™ subscription"
但仍然在电子邮件主题中它仍然显示为 "Your new InCites™ subscription"
采纳答案by Roland Illig
The subject you mention here consists completely of ASCII characters. This includes the funny special characters ™
. If you want it to be Unicode, just use Unicode and not the HTML-escaping. Mails don't have anything to do with HTML.
您在这里提到的主题完全由 ASCII 字符组成。这包括有趣的特殊字符™
。如果您希望它是 Unicode,只需使用 Unicode 而不是 HTML 转义。邮件与 HTML 没有任何关系。
mimemsg.setSubject("Your new InCites\u2122 subscription", "UTF-8");
This should encode the subject as something like =?UTF-8?Q?Your...subscription?=
, as specified in RFC 2047.
这应该将主题编码为类似于RFC 2047 中=?UTF-8?Q?Your...subscription?=
指定的内容。
The complete example code:
完整的示例代码:
package so4406538;
import java.io.IOException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
public class MailDemo {
public static void main(String[] args) throws MessagingException, IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setSubject("Your new InCites\u2122 subscription", "UTF-8");
message.setContent("hello", "text/plain");
message.writeTo(System.out);
}
}
The output:
输出:
Message-ID: <7888229.0.1291967222281.JavaMail.roland@bacc>
Subject: =?UTF-8?Q?Your_new_InCites=E2=84=A2_subscription?=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
hello
You can see that the subject header is encoded, and this is necessary and correct.
您可以看到主题标头已编码,这是必要且正确的。
[Update:I fixed the Unicode escape sequence, as indicated in one of my comments.]
[更新:我修复了 Unicode 转义序列,如我的评论之一所示。]