Apache commons-Email中文问题的解决办法

Apache commons-email是对javamailAPI的一层封装,经封装后的发送邮件的代码变得极为简单,但这里有一个中文支持的小问题。
commons-email主要的封装类是Email类,这是一个抽象类,该框架给出了SimpleEmail的默认实现,但该实现并不支持中文,即使调用Email的setCharset也不起作用。
事 实上,SimpleEmail调用了Email超类中的setContent方法来设置邮件内容(通过setMsg方法),而在设置内容时,又采用了默认 的英文字符集,我们只要在代码中直接调用email类的setContent方法就可以支持中文了,但要注意setContent具备两个参数,第一个是 内容对象,第二个则是内容类型,我们把第二个参数设置为:
SimpleEmail.TEXT_PLAIN + "; charset=utf-8", 即可。理由如下面源代码所示:
java 代码
 
  1. public void setContent(Object aObject, String aContentType)  
  2.     {  
  3.           ......  
  4.             // set the charset if the input was properly formed  
  5.             String strMarker = "; charset=";  
  6.             int charsetPos = aContentType.toLowerCase().indexOf(strMarker);  
  7.   
  8.             if (charsetPos != -1)  
  9.             {  
  10.                 // find the next space (after the marker)  
  11.                 charsetPos += strMarker.length();  
  12.                 int intCharsetEnd =  
  13.                     aContentType.toLowerCase().indexOf(" ", charsetPos);  
  14.   
  15.                 if (intCharsetEnd != -1)  
  16.                 {  
  17.                     this.charset =  
  18.                         aContentType.substring(charsetPos, intCharsetEnd);  
  19.                 }  
  20.                 else  
  21.                 {  
  22.                     this.charset = aContentType.substring(charsetPos);  
  23.                 }  
  24.             }  
  25.         }  
  26.     }  
即有一个文本解析的过程。即有一个文本解析的过程。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值