Announcement Announcement Module
Collapse
No announcement yet.
MailPreparationException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MailPreparationException

    hello all,

    i want to submit a form with file parameters, and send a email whitn attachment, i have do this:
    service:
    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);
        }
      }
    }
    web:

    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;
      }
    }
    i can't use multipartResolver because my server is J2EE 1.2.
    the server lanch exception:
    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
    how can i do this??? what am i doing wrong???

    thanks in advance,
    CÚsar.

  • #2
    Seems that your InputStream is read more than once by the mail parser. I recommend to not work with InputStreams respectively InputStreamResource in the first place: Rather, use a List of org.springframework.core.io.Resource objects, which can be directly passed to addAttachment and do support multiple read attempts.

    Juergen

    Comment


    • #3
      thanks Juergen,

      but i don┤t know how not work with InputStream?
      Code:
      DiskFileUpload upload = new DiskFileUpload();
      List items = upload.parseRequest(request);
      Iterator iter = items.iterator();
      while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();
        InputStream in = item.getInputStream();
        InputStreamSource attachment = new InputStreamResource(in);
        ...
      how can i get the file of FileItem and create a Resource to message.addAttachment(...)?

      thanks,
      CÚsar.

      Comment

      Working...
      X