james 邮件显示正文+图片的cid

邮件发送的时候添加cid:图片显示路径;显示的时候去掉.replaceAll("cid:","");不用下载到本地再替换而来。
1.
//处理带正文的图片的信息rex获取《img src》
public List filterImage(String content,HttpServletRequest request) throws MalformedURLException{
StringBuffer images = new StringBuffer();
String text = content;
String path= request.getSession().getServletContext().getRealPath("/");
int pos=path.lastIndexOf("\\", path.length()-2);
path=path.substring(0, pos+1);
/**过滤img,直取src的值*/
/**String regEx="<img\\s+(?:[^>]*)src\\s*=\\s*([^>]+)>";*/
//String regex = "<\\s*img[^>]*?>";
String regex = "<\\s*img[^>]+src\\s*=\\s*['\"]([^\"'>]+)['\"][^>]*>";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(text);
while(m.find()) {
images.append(m.group(1));
images.append(",");
}
List imgPart= new ArrayList();


String [] returnUrl = null;

if(images.toString().length()>0){


String urls=images.toString().substring(0,images.toString().length()-1);
String [] url = urls.split(",");
returnUrl = new String[url.length];


for(int i=0;i<url.length;i++){
BodyPart img= new MimeBodyPart();
DataHandler dh =new DataHandler(new FileDataSource(path+url[i]));
try {
img.setDataHandler(dh);
img.setHeader("Content-ID", url[i]);//绝对路径的问题
returnUrl[i]="cid:"+url[i];
imgPart.add(img);//将图片处理之后再作为发信的内容
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

String [] imagesFlaseUrl = images.toString().split(",");
String sb = text;


for(int j=0;j<returnUrl.length;j++){
sb = sb.replace(imagesFlaseUrl[j], returnUrl[j]);
}
imgPart.add(sb);//最后将string处理《img src="cid:路径" 》
}else {

imgPart.add(content);//没有图片 》
}


return imgPart;



}
2.正文显示的时候去掉:MimeUtility.decodeText(content).replaceAll("cid:", "")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值