Javamail中的常见中文乱码问题与解决办法

发送名称含中文的附件到邮件服务器,用别的邮件接收程序接收到的附件名显示为乱码解决办法:在调用MimeBodyPart的setFileName()时使用Base64编码。例如:

    BASE64Encoder enc = newBASE64Encoder();//该类位于jre/lib/rt.jar中//fds为FileDataSource实例mbp.setFileName(“=?GBK?B?”enc.encode((fds.getName()).getBytes()) “?=”);

接收邮件时,获取某些邮件发送程序发送的email地址,发送地址显示为乱码解决办法:对含有中文的发送地址,使用MimeUtility.decodeTex方法,对其他则把地址从ISO8859_1编码转换成gbk编码,见下例

    publicstaticStringgetFrom(Message msg){ Stringfrom=””; try{ if(msg.getFrom()[0]!=null) from=msg.getFrom()[0].toString(); if(from.startsWith(“=?GB”)||from.startWith(“=?gb”)){ from=MimeUtility.decodeText(from); }else{ from=StringUtil.toChinese(from); } }catch(Exceptione){ e.printStackTrace(); } from=StringUtil.replaceStr(from,“<”,“<”);// replaceStr为字符串替换函数from=StringUtil.replaceStr(from,”>”,”>”); returnfrom; } ///////////////////StringUtil的toChinese方法//////////////////////////publicstaticStringtoChinese(Stringstrvalue){ try{ if(strvalue==null) returnnull; else{ strvalue = newString(strvalue.getBytes(“ISO8859_1”), “GBK”); returnstrvalue; } }catch(Exceptione){ returnnull; } }

因为你的喜爱会挡也挡不住地流露出来。

Javamail中的常见中文乱码问题与解决办法

相关文章:

你感兴趣的文章:

标签云: