spring mail中附件名乱码问题

来源:互联网 发布:vb怎样弹出对话框 编辑:程序博客网 时间:2024/05/19 06:50

           MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");                // use the true flag to indicate you need a multipart message
           helper.setTo(recipients);
           if (sender == null) {
               helper.setFrom(defaultFrom);   // use the default sending if no sender specified
           } else {
              helper.setFrom(sender);
           }
           helper.setSubject(subject);
           helper.setText(bodyText,true);  //true表示内容解析成html

     for(Iterator it=attachmentMap.entrySet().iterator();it.hasNext();){
         Map.Entry entry = (Map.Entry) it.next();
         String fileName= entry.getKey().toString();
         String filePath= entry.getValue().toString();

            FileSystemResource file =new FileSystemResource( new File(filePath) );  //读取附件
            helper.addAttachment(MimeUtility.encodeWord(fileName),file);    //向email中添加附件,MimeUtility.encodeWord(fileName)解决附件名乱码问题
     }
           ((JavaMailSenderImpl) mailSender).send(message);