
Originally Posted by
Juergen Hoeller
First of all, you don't need to create a class like MyInputStream: If you want to pass an existing InputStream in, use Spring's InputStreamResource - which implements the InputStreamSource interface, wrapping a passed-in InputStream.
Juergen
I have tried to do use the InputStream and I get a exception I can't resolve it ,so I create a new inner class which implements the InputStreamSource interface wrapping a passed-in InputSteam.
the Exception is:
InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
the below is my code:
Code:
MultipartHttpServletRequest request = (MultipartHttpServletRequest) rst;
MultipartFile file1 = request.getFile("attachment1");
MultipartFile file2 = request.getFile("attachment2");
String text=request.getParameter("text");
String subject=request.getParameter("subject");
List enterprises=null;
//给组的成员发送信息
String groupId=request.getParameter("groupId");
if(groupId!=null&&!groupId.equals("")){
Group group=(Group)this.getGroupDao().load(groupId);
enterprises=this.getEnterpriseDao().findByGroup(groupId);
for(int i=0;i<enterprises.size();i++){
CustomerEntity customerEntity=(CustomerEntity)enterprises.get(i);
//给每一个客户发送邮件
MimeMessage message = this.getJavaMailSender().createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom("test@tom.com");
helper.setTo(customerEntity.getEmail());
helper.getEncoding();
// use the true flag to indicate the text included is HTML
helper.setText(text);
helper.setSubject(subject);
if(file1.getSize()!=0){
InputStreamResource s=new InputStreamResource (file1.getInputStream(),"测试1");
helper.addAttachment(file1.getOriginalFilename(),s);
}
if(file2.getSize()!=0){
InputStreamResource t=new InputStreamResource (file2.getInputStream(),"测试2");
helper.addAttachment(file2.getOriginalFilename(),t);
}
try{
this.getJavaMailSender().send(message);
}catch(Exception e){
// throw new SendMailException();
e.printStackTrace();
}
}
return new ModelAndView(new RedirectView("detailGroup.sf?groupId="+groupId),"mailInfo","true");
could anyone tell me why?The Exception tell us that the InputStream couldnt' be read for many times.so are there any other methods to deal with this problem? and could anybody share his code as a example.The method I have used could send Email with attachment but with wrong encoding. and I have two attachment in my form. so what should do?
thanks in advance.