javamail 读邮件的中文乱码问题 subject <%= messages[i].getSubject() %>body:<%=new String(messages[i].getContent().toString().getBytes("gb2312"),"ISO8859_1")%> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我试过在jsp里显示,仍然有乱码以下是邮件源码,只有邮件头部分:这一封内容类型是text/plain,用String subject = message.getSubject();就可以正常显示中文主题Received: from wm2.163.com (bj222.163.com [192.168.1.222]) by sm203.163.com (Postfix) with ESMTP id 079E21C98C4C1 for <[email protected]>; Tue, 3 Dec 2002 18:56:03 +0800 (CST)Received: by wm2.163.com (Postfix, from userid 60001) id 3E59E1C9FA0DB; Tue, 3 Dec 2002 18:54:28 +0800 (CST)MIME-Version: 1.0Message-ID: <[email protected]>Date: Tue, 3 Dec 2002 18:54:28 +0800 (CST)From: [email protected]To: [email protected]Subject: =?gb2312?B?16LS4rbNwbbJ7czl?=X-Priority: 3X-Originating-IP: [61.139.139.231]X-Mailer: Coremail2.0 Copyright Tebie Ltd., 2001这一封内容类型是text/plain,须经过 subject = new String((subject.getBytes("iso-8859-1")),"gb2312");处理才能正确显示中文Received: from localhost (localhost [127.0.0.1]) by mx7.163.com (Postfix) with SMTP id 266AD1CACF832 for <[email protected]>; Wed, 21 Aug 2002 17:06:50 +0800 (CST)Received: from free.100ok.com (unknown [211.157.102.18]) by 192.168.1.229 (Coremail) with SMTP id wgYAACpYYz0mAWYS.1 for <[email protected]>; Wed, 21 Aug 2002 17:06:50 +0800 (CST)Received: (qmail 11408 invoked from network); 21 Aug 2002 08:57:36 -0000Received: from unknown (HELO AspEmail) (211.157.102.21) by 0 with SMTP; 21 Aug 2002 08:57:36 -0000From: "CSDN通知" <[email protected]>To: [email protected]Reply-To: CSDN通知@163.com, 此信不要回,该信箱无人接收,谢谢@163.comSUBJECT:CSDN论坛Message-Id: <[email protected]>Date: Wed, 21 Aug 2002 17:06:50 +0800 (CST)这一封内容是multipart/mixed;须经过subject = new String((subject.getBytes("iso-8859-1")),"gb2312");处理才能正确显示中文Received: by wm6.163.com (Postfix, from userid 60001) id BDE841CF54B50; Mon, 11 Nov 2002 09:59:48 +0800 (CST)Subject: Fw: (by [email protected])来了呀 Received: from localhost (localhost [127.0.0.1]) by mx1.163.com (Postfix) with SMTP id 70AC41C494DCA for <[email protected]>; Fri, 8 Mar 2002 23:28:32 +0800 (CST)Received: from mx01.x263.net (unknown [211.157.130.62]) by 192.168.1.207 (Coremail) with SMTP id 0QMAAJ/YiDxgAII+.1 for <[email protected]>; Fri, 08 Mar 2002 23:28:32 +0800 (CST)Received: by mx01.x263.net (Postfix, from userid 500) id AA002A4A26; Fri, 8 Mar 2002 23:21:04 +0800 (CST)From: [email protected]To: [email protected]Subject: =?gb2312?B?wLTBy9G9IA==?=Date: Fri, 8 Mar 2002 23:22:52 +0800X-Priority: 3X-MSMail-Priority: NormalMIME-Version: 1.0X-Mailer: XMail-1.0Content-Type: multipart/mixed; boundary="----=NextPart_263Mail-XMAIL-000-000-1015600972Wa1"Message-Id: <[email protected]>Sender: [email protected]从源码看来,javamail可以识别=?gb2312?格式的中文,(我不太懂这种编码,好像是base64?),但是同时简单文本的中文就会识别错误.而且取得主题的方法只有message.getSubject(),得到String 之后,就好像没有办法判断它的编码格式了.在这之前有办法判断subject的编码格式吗?我试过按邮件内容的类型来判断,不过从上边第2,3封信来看,也不准.请继续帮忙,谢谢 ...自己找到了一个解决办法,但是有点笨,肯定不是最好的, subject = message.getSubject(); String header = ((MimeMessage)message).getHeader("SUBJECT")[0]; if (!(header.toLowerCase().startsWith("=?gb2312?"))) { subject = new String((subject.getBytes("iso-8859-1")),"gb2312"); }这样处理过的subject输出的时候不会出现中文乱码.希望有高人提出更好的办法 Content-Type: text/html;charset="GB2312"email中应该有这项,按照charset进行转换 代码应该改为以下才能具有通用性:subject = message.getSubject(); String header = ((MimeMessage)message).getHeader("SUBJECT")[0]; if ((header.toLowerCase().indexOf("=?"))>0) { subject = new String((subject.getBytes("iso-8859-1")),"gb2312"); }请参考rfc 楼上老兄的方法.我试过了.不管是够以"=?"开头,总是会执行if里面的语句.liuxy999的方法不错.顶一下! javax.mail.internet.MimeUtility.decode(message.getSubject());javax.mail.internet.MimeUtility.decode(message.getContent()); 错了,应该是:javax.mail.internet.MimeUtility.decodeText(message.getSubject());javax.mail.internet.MimeUtility.decodeText(message.getContent()); 这里有个更好的方法http://www.chinabs.net/java/default.asp?infoid=225 to zsh99(白粥): 如果不是BASE64编码方式怎么办,如Quote-Printable编码方式.MimeUtility.decodeText才是好方法。 import java.io.UnsupportedEncodingException;import javax.mail.internet.MimeUtility;import javax.mail.internet.ParseException;public class MimeTest2{ public static void main(String[] args) { try { testEncode(); System.out.println(); testDecode(); } catch (Exception e) { e.printStackTrace(); } } private static void testEncode() throws UnsupportedEncodingException { String s = "if(s.equalsIgnoreCase(binary) 中文 || s.equalsIgnoreCase(7bit) || s.equalsIgnoreCase(8bit)) aaaaaaaaaaa bbbbbbbbbbbbbb cccccccccccc"; System.out.println(MimeUtility.encodeText(s)); System.out.println(MimeUtility.encodeText("中文")); } private static void testDecode() throws UnsupportedEncodingException, ParseException { String s = "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n=?ISO-8859-1?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?="; System.out.println(MimeUtility.decodeText(s)); System.out.println(MimeUtility.decodeText("=?GBK?B?1tDOxA==?=")); }} 新手求教JAVASE关于Flash调用的问题 在发一道面试逻辑思维题(明天公布答案) j2se做桌面应用合适吗? MissingResourceException JMF的问题 设置classpath时可以一次设置多个jar吗,比如在这些jar在d:\lib文件夹中,可以直接设置classpath=d:\lib吗 用IE打开Applet读本地文件,没权限!如何处理? (转) 中专学历的我面试华为的经历 关于继承的一个小问题 无线互联,一片美好! 对Java的一点疑惑,欢迎大家讨论 在jdbTable 中,如何指定view中的主键?
以下是邮件源码,只有邮件头部分:这一封内容类型是text/plain,用String subject = message.getSubject();就可以正常显示中文主题
Received: from wm2.163.com (bj222.163.com [192.168.1.222])
by sm203.163.com (Postfix) with ESMTP id 079E21C98C4C1
for <[email protected]>; Tue, 3 Dec 2002 18:56:03 +0800 (CST)
Received: by wm2.163.com (Postfix, from userid 60001)
id 3E59E1C9FA0DB; Tue, 3 Dec 2002 18:54:28 +0800 (CST)
MIME-Version: 1.0
Message-ID: <[email protected]>
Date: Tue, 3 Dec 2002 18:54:28 +0800 (CST)
From: [email protected]
To: [email protected]
Subject: =?gb2312?B?16LS4rbNwbbJ7czl?=
X-Priority: 3
X-Originating-IP: [61.139.139.231]
X-Mailer: Coremail2.0 Copyright Tebie Ltd., 2001
这一封内容类型是text/plain,须经过
subject = new String((subject.getBytes("iso-8859-1")),"gb2312");
处理才能正确显示中文Received: from localhost (localhost [127.0.0.1])
by mx7.163.com (Postfix) with SMTP id 266AD1CACF832
for <[email protected]>; Wed, 21 Aug 2002 17:06:50 +0800 (CST)
Received: from free.100ok.com (unknown [211.157.102.18])
by 192.168.1.229 (Coremail) with SMTP id wgYAACpYYz0mAWYS.1
for <[email protected]>; Wed, 21 Aug 2002 17:06:50 +0800 (CST)
Received: (qmail 11408 invoked from network); 21 Aug 2002 08:57:36 -0000
Received: from unknown (HELO AspEmail) (211.157.102.21)
by 0 with SMTP; 21 Aug 2002 08:57:36 -0000
From: "CSDN通知" <[email protected]>
To: [email protected]
Reply-To: CSDN通知@163.com, 此信不要回,该信箱无人接收,谢谢@163.com
SUBJECT:CSDN论坛
Message-Id: <[email protected]>
Date: Wed, 21 Aug 2002 17:06:50 +0800 (CST)
这一封内容是multipart/mixed;须经过
subject = new String((subject.getBytes("iso-8859-1")),"gb2312");
处理才能正确显示中文Received: by wm6.163.com (Postfix, from userid 60001)
id BDE841CF54B50; Mon, 11 Nov 2002 09:59:48 +0800 (CST)
Subject: Fw: (by [email protected])来了呀
Received: from localhost (localhost [127.0.0.1])
by mx1.163.com (Postfix) with SMTP id 70AC41C494DCA
for <[email protected]>; Fri, 8 Mar 2002 23:28:32 +0800 (CST)
Received: from mx01.x263.net (unknown [211.157.130.62])
by 192.168.1.207 (Coremail) with SMTP id 0QMAAJ/YiDxgAII+.1
for <[email protected]>; Fri, 08 Mar 2002 23:28:32 +0800 (CST)
Received: by mx01.x263.net (Postfix, from userid 500)
id AA002A4A26; Fri, 8 Mar 2002 23:21:04 +0800 (CST)
From: [email protected]
To: [email protected]
Subject: =?gb2312?B?wLTBy9G9IA==?=
Date: Fri, 8 Mar 2002 23:22:52 +0800
X-Priority: 3
X-MSMail-Priority: Normal
MIME-Version: 1.0
X-Mailer: XMail-1.0
Content-Type: multipart/mixed;
boundary="----=NextPart_263Mail-XMAIL-000-000-1015600972Wa1"
Message-Id: <[email protected]>
Sender: [email protected]
从源码看来,javamail可以识别=?gb2312?格式的中文,(我不太懂这种编码,好像是base64?),但是同时简单文本的中文就会识别错误.而且取得主题的方法只有message.getSubject(),得到String 之后,就好像没有办法判断它的编码格式了.在这之前有办法判断subject的编码格式吗?我试过按邮件内容的类型来判断,不过从上边第2,3封信来看,也不准.
请继续帮忙,谢谢
{
subject = new String((subject.getBytes("iso-8859-1")),"gb2312");
}这样处理过的subject输出的时候不会出现中文乱码.
希望有高人提出更好的办法
email中应该有这项,按照charset进行转换
{
subject = new String((subject.getBytes("iso-8859-1")),"gb2312");
}请参考rfc
liuxy999的方法不错.顶一下!
javax.mail.internet.MimeUtility.decode(message.getContent());
javax.mail.internet.MimeUtility.decodeText(message.getSubject());
javax.mail.internet.MimeUtility.decodeText(message.getContent());
http://www.chinabs.net/java/default.asp?infoid=225
如果不是BASE64编码方式怎么办,如Quote-Printable编码方式.MimeUtility.decodeText才是好方法。
import javax.mail.internet.MimeUtility;
import javax.mail.internet.ParseException;public class MimeTest2
{
public static void main(String[] args)
{
try
{
testEncode();
System.out.println();
testDecode();
}
catch (Exception e)
{
e.printStackTrace();
}
} private static void testEncode() throws UnsupportedEncodingException
{
String s = "if(s.equalsIgnoreCase(binary) 中文 || s.equalsIgnoreCase(7bit) || s.equalsIgnoreCase(8bit)) aaaaaaaaaaa bbbbbbbbbbbbbb cccccccccccc";
System.out.println(MimeUtility.encodeText(s));
System.out.println(MimeUtility.encodeText("中文"));
} private static void testDecode() throws UnsupportedEncodingException, ParseException
{
String s = "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r\n=?ISO-8859-1?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=";
System.out.println(MimeUtility.decodeText(s));
System.out.println(MimeUtility.decodeText("=?GBK?B?1tDOxA==?="));
}
}