[英]EMail subject get set in CP1252 encoding even though the string is in UTF-8
The file new1.txt contains UTF-8 string which is sent as subject. 文件new1.txt包含作为主题发送的UTF-8字符串。 But the received email will show the string in CP1252 format.
但是收到的电子邮件将以CP1252格式显示字符串。 But if I set console encoding by going to Run Config->Common Tab and set console encoding as UTF-8, I can see UTF-string properly in the recieved email.
但是,如果我通过转到“运行配置”->“通用选项卡”来设置控制台编码并将控制台编码设置为UTF-8,则可以在收到的电子邮件中正确看到UTF字符串。 I using google server for this test:
我使用Google服务器进行此测试:
Not working: 日本 : Partner Name: アストラゼãƒ?ã‚«æ ªå¼?会社 : 不起作用:日本:合作伙伴名称:ã,¢ã‚¹ƒˆãƒ©ã‚¼ãƒ?ã,«æªå¼?会社:
Working: 日本 : Partner Name: アストラゼネカ株式会社 工作:日本:合作伙伴名称:アストラゼネカ株式会社
My Code: final String username = "xxxxx@gmail.com"; 我的代码:最终的字符串用户名=“ xxxxx@gmail.com”; final String password = "xxxxxx";
最终的字符串密码=“ xxxxxx”;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
try {
File fileDir = new File("c:\\new1.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF-8"));
String str;
String str1 ="";
while ((str = in.readLine()) != null) {
str1 += str;
}
Message message = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
message.setFrom(new InternetAddress("xxxxx@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xxxx@xxxxx.com"));
message.setSubject(str1);
MimeBodyPart body = new MimeBodyPart();
body.setContent("This is a Test EMail. Please ignore", "text/html");
body.setDisposition(MimeBodyPart.INLINE);
mp.addBodyPart(body);
byte[] attachmentData = str1.getBytes();
DataHandler dh = new DataHandler(new ByteArrayDataSource(attachmentData,"application/octet-stream"));
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(dh);
attachment.setDisposition(MimeBodyPart.ATTACHMENT);
attachment.setFileName("new1.txt");
mp.addBodyPart(attachment);
message.setContent(mp);
Transport.send(message);
System.out.println("Done");
thanks Madhu 感谢Madhu
Following are the code changes I made in my J2EE app. 以下是我在J2EE应用程序中所做的代码更改。 In this it is adding 3 files as attachments.
在此添加了3个文件作为附件。 One of the 3 files is the subject itself.
3个文件之一是主题本身。 In the attachment (Subject.txt) conent is fine.
在附件(Subject.txt)中,同意可以。
MimeMessage message = new MimeMessage(s);
....
message.setSubject(subject,"UTF-8");// MimeUtility.encodeText(subject,"UTF-8", "B"));
...
MimeBodyPart body = new MimeBodyPart();
body.setContent(sm.getMailBody(), "text/html; charset=UTF-8");
body.setDisposition(MimeBodyPart.INLINE);
mp.addBodyPart(body);
The debug message. 调试消息。 I could not add it as text
我无法将其添加为文本
Subject would need to be encoded using MimeUtility.encodeText() . 主体需要使用MimeUtility.encodeText()进行编码。
But as Bill Shannon pointed out: Don't do this by encoding it manually, call MimeMessage.setSubject(String subject, String charset)
instead. 但是正如Bill Shannon所指出的那样:不要通过手动编码来做到这一点,而是调用
MimeMessage.setSubject(String subject, String charset)
。 Otherwise setSubject will internally encode resulting string with platform encoding which might cause trouble. 否则,setSubject将使用平台编码对内部生成的字符串进行内部编码,这可能会引起麻烦。
One more thing: 还有一件事:
byte[] attachmentData = str1.getBytes();
Retrieves bytes in platform encoding - which might not be utf-8. 以平台编码检索字节-可能不是utf-8。 Try
尝试
byte[] attachmentData = str1.getBytes(Charset.forName("utf-8"));
将Message声明为MimeMessage,然后使用setSubject方法允许您指定字符集 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.