天天看点

java中文乱码decode_Javamail中的常见中文乱码问题与解决办法(综合)

接收邮件时,获取某些邮件发送程序发送的email地址,发送地址显示为乱码

public static String getFrom(Message msg){

String from="";

try{

if(msg.getFrom()[0]!=null)

from=msg.getFrom()[0].toString();

if(from.startsWith("=?GB")||from.startsWith("=?gb")){

from=MimeUtility.decodeText(from);

}else{

from=StringUtils.toChinese(from);

}

}catch(Exception e){

e.printStackTrace();

}

return from;

}

StringUtils类方法

public static String toChinese(String strvalue){

try{

if(strvalue==null)

return null;

else{

strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");

return strvalue;

}

}catch(Exception e){

return null;

}

}

public static String getFromBASE64(String s) {

if (s == null)

return null;

try {

byte[] b = Base64.decodeBase64(s);

return new String(b);

} catch (Exception e) {

return null;

}

}