有熟悉javamail发送邮件(带附件的)吗?附件名称显示的乱码问题 搞定了,把messageBodyPart.setFileName(source.getName());改成 messageBodyPart.setFileName(MimeUtility.encodeText(source.getName()));就行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 恭喜楼上啊,MimeUtility.encodeText(source.getName()) Javamail中的常见中文乱码问题与解决办法(综合)在使用javamail api开发邮件服务系统时,我们常常会碰到很多中文乱码问题,下面就分别介绍如何解决这些问题。1.发送名称含中文的附件到邮件服务器,用别的邮件接收程序接收到的附件名显示为乱码解决办法:在调用MimeBodyPart的setFileName()时使用Base64编码。例如:BASE64Encoder enc = new BASE64Encoder();//该类位于jre/lib/rt.jar中 //fds为FileDataSource实例 mbp.setFileName("=?GBK?B?"+enc.encode((fds.getName()).getBytes())+"?=");2.接收邮件时,获取某些邮件发送程序发送的email地址,发送地址显示为乱码解决办法:对含有中文的发送地址,使用MimeUtility.decodeTex方法,对其他则把地址从ISO8859_1编码转换成gbk编码,见下例public static String getFrom(Message msg){ String from=""; 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(Exception e){ e.printStackTrace(); } from=StringUtil.replaceStr(from,“<”,“<”);// replaceStr为字符串替换函数 from=StringUtil.replaceStr(from,">",">"); return from; } ///////////////////StringUtil的toChinese方法////////////////////////// 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; } }3.接收邮件时,获取某个邮件的中文附件名,出现乱码解决办法:对于用base64编码过的中文,则采用base64解码,否则对附件名进行ISO8859_1到gbk的编码转换,例如:String temp=part.getFileName();//part为Part实例 if((temp.startsWith("=?GBK?B?")&&temp.endsWith("?=")) ||(temp.startsWith("=?gbk?b?")&&temp.endsWith("?="))){ temp=StringUtil.getFromBASE64(temp.substring(8,temp.indexOf("?=")-1)); }else{ temp=StringUtil.toChinese(temp);//该方法如前所叙 } /////////////StringUtil的getFromBASE64方法///////// public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }乱码问题的调试步骤总结:基本上在javamail中碰到的中文乱码问题就这么多了,如果你的程序出现了中文乱码,首先不要惊慌,可用多个其他的邮件发送或接收程序进行验证,看是在哪个环节出现了问题,然后再仔细对照原文和乱码,调用相应的编码解码方法就行了。最后,希望这篇短文能对你有所启发,祝你成功。 Js 打印问题 单例模式在struts2环境下的应用 javaweb开发的BBS系统在发表帖子的也面出现这样的错误 求教 struts2在jsp中如何随机访问集合(集合可能是Array,List、Set中的一种)属性的元素?(注意:不是遍历,是随机访问) hibernate关联表问题 插件问题 那位经验丰富的高人回答下这个问题 如何得到系统的时间,再将它写入数据库中呢? 关于使用weblogic7的问题 如何判断一个用户提交的表单的写法是合法的!例如:密码只是数字的,用户名是字母的!! 请教如何利用Java Excel API对单元格设置批注? 请教高手:关于打印预览问题
在使用javamail api开发邮件服务系统时,我们常常会碰到很多中文乱码问题,下面就分别介绍如何解决这些问题。1.发送名称含中文的附件到邮件服务器,用别的邮件接收程序接收到的附件名显示为乱码解决办法:在调用MimeBodyPart的setFileName()时使用Base64编码。例如:
BASE64Encoder enc = new BASE64Encoder();//该类位于jre/lib/rt.jar中
//fds为FileDataSource实例
mbp.setFileName("=?GBK?B?"+enc.encode((fds.getName()).getBytes())+"?=");
2.接收邮件时,获取某些邮件发送程序发送的email地址,发送地址显示为乱码解决办法:对含有中文的发送地址,使用MimeUtility.decodeTex方法,对其他则把地址从ISO8859_1编码转换成gbk编码,见下例
public static String getFrom(Message msg)
{ String from="";
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(Exception e){ e.printStackTrace(); }
from=StringUtil.replaceStr(from,“<”,“<”);// replaceStr为字符串替换函数 from=StringUtil.replaceStr(from,">",">");
return from; }
///////////////////StringUtil的toChinese方法//////////////////////////
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; }
}
3.接收邮件时,获取某个邮件的中文附件名,出现乱码解决办法:对于用base64编码过的中文,则采用base64解码,否则对附件名进行ISO8859_1到gbk的编码转换,例如:
String temp=part.getFileName();//part为Part实例
if((temp.startsWith("=?GBK?B?")&&temp.endsWith("?=")) ||(temp.startsWith("=?gbk?b?")&&temp.endsWith("?=")))
{ temp=StringUtil.getFromBASE64(temp.substring(8,temp.indexOf("?=")-1));
}else
{ temp=StringUtil.toChinese(temp);//该方法如前所叙
} /////////////StringUtil的getFromBASE64方法/////////
public static String getFromBASE64(String s)
{
if (s == null) return null;
BASE64Decoder decoder = new BASE64Decoder();
try
{
byte[] b = decoder.decodeBuffer(s);
return new String(b);
} catch (Exception e) { return null; }
}
乱码问题的调试步骤总结:基本上在javamail中碰到的中文乱码问题就这么多了,如果你的程序出现了中文乱码,首先不要惊慌,可用多个其他的邮件发送或接收程序进行验证,看是在哪个环节出现了问题,然后再仔细对照原文和乱码,调用相应的编码解码方法就行了。
最后,希望这篇短文能对你有所启发,祝你成功。