JavaMail中标题乱码的问题!

huwin 2003-01-10 10:36:23
我用javamail中的mm.getSubject()方法得到的信件标题,再不事先标记为GB2312时,返回的中文标题会是乱码,而且使用:
String subject = new String(mm.getSubject().getBytes("ISO-8859-1"),"GB2312");转换也无效。

例如用sina.com.cn的webmail发出的邮件,请问如何解决呢?
...全文
210 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
54sunboy 2003-01-13
  • 打赏
  • 举报
回复
謝謝!
GJA106 2003-01-10
  • 打赏
  • 举报
回复
public String getSubject()
throws MessagingException
{
String strSubject = "",temp="";
int pos = 0;
String strMailSubject[] = message.getHeader("Subject");
boolean flag = MailAccount.A;
String strCodingType = "";
if(strMailSubject == null)
{// System.out.println("邮件头空值,返回[没有主题]");
return subject;
}
else
{// System.out.println("原始长度 "+strMailSubject.length+" 内容 "+strMailSubject[0]);
if(strMailSubject[0].indexOf("=?GB_2312-80?") != -1)
strMailSubject[0] = StringUtil.replaceStr(strMailSubject[0], "=?GB_2312-80?", "=?GBK?");
try
{
if(strMailSubject[0].indexOf("=?ISO-8859-1?") != -1)
{
System.out.println("subject after decodeText =" + strMailSubject[0]);
strMailSubject[0] = new String(strMailSubject[0].getBytes("ISO-8859-1"));
}
else if(strMailSubject[0].indexOf("=?gb2312?") != -1)
{
strMailSubject[0] = new String(strMailSubject[0].getBytes("gb2312"));
}
else if(strMailSubject[0].indexOf("=?GB2312?") != -1)
{
strMailSubject[0] = new String(strMailSubject[0].getBytes("gb2312"));
}
else if(strMailSubject[0].indexOf("=?GBK?") != -1)
{
strMailSubject[0] = new String(strMailSubject[0].getBytes("GBK"));
}
else if(strMailSubject[0].indexOf("=?big5?") != -1)
strMailSubject[0] = new String(strMailSubject[0].getBytes("big5"));
else
{//正常字符集:ISO8859_1-->gb2312;主题ISO-8859-1-->gb2312;ISO-8859-1-->GBK;
strMailSubject[0] = new String(strMailSubject[0].getBytes("ISO-8859-1"), "gb2312");
}
strMailSubject[0] = MimeUtility.decodeText(strMailSubject[0]);
}
catch(UnsupportedEncodingException unsupportedencodingexception)
{
unsupportedencodingexception.printStackTrace();
cat.error("UnsupportedEncoding", unsupportedencodingexception);
}

}

this.subject = strMailSubject[0];
if(subject == null)
subject = "";
return subject;
}
wks9527 2003-01-10
  • 打赏
  • 举报
回复
void setSubject(String subject) throws Exception{
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
msg.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");
}

用这个应该可能解决
beming 2003-01-10
  • 打赏
  • 举报
回复
试一下不用GB2312(直接转成ISO8859-1)或者用GBK(有没有可能是繁体)。

http://www.csdn.net/develop/read_article.asp?id=16102

http://www.csdn.net/develop/read_article.asp?id=16362

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧