接收郵件時,獲取某些郵件發送程序發送的email地址,發送地址顯示爲亂碼code
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類方法get
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; } }