hello all,
i want to submit a form with file parameters, and send a email whitn attachment, i have do this:
service:
web:Code:public class MailManagerImpl implements MailManager { ... private JavaMailSender mailSender; public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendMail(...) { MimeMessagePreparator messagePreparator = new MyMessagePreparator(...); this.mailSender.send(messagePreparator); } } class MyMessagePreparator implements MimeMessagePreparator { private String encoding; private String[] arrayTo; private String from; private String subject; private String text; private List attachments; public MyMessagePreparator(...) { ... } public void prepare(MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, encoding); message.setCc(from); message.setTo(arrayTo); message.setSubject(subject); message.setText(text()); Iterator iterAttachments = attachments.iterator(); while (iterAttachments.hasNext()) { InputStream inAttachment = (InputStream) iterAttachments.next(); InputStreamSource attachment = new InputStreamResource(inAttachment); //new InputStreamResource() message.addAttachment(value, attachment); } } }
i can't use multipartResolver because my server is J2EE 1.2.Code:public class MailController extends MultiActionController { private MailManager mailManager; public void setMailManager(MailManager mailManager) { this.mailManager = mailManager; } public ModelAndView handleSendMail(HttpServletRequest request, HttpServletResponse response) throws Exception { try { List listAttachments = parseForm(request); this.mailManager.sendMail(..., listAttachments); return new ModelAndView("resultView"); } catch (Exception e) { ... } } private List parseForm(HttpServletRequest request) throws ServletException, FileUploadException, IOException { List listAttachments = new ArrayList(); DiskFileUpload upload = new DiskFileUpload(); List items = upload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.getSize()!= 0L) { String name = item.getFieldName(); if (item.isFormField()) { String value = item.getString(); ... } else { InputStream inStream = item.getInputStream(); listAttachments.add(inStream); } } } return listAttachments; } }
the server lanch exception:
how can i do this??? what am i doing wrong???Code:ERROR - org.springframework.mail.MailPreparationException: Could not prepare mail: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times; nested exception is java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
thanks in advance,
César.


Reply With Quote