Announcement Announcement Module
Collapse
No announcement yet.
Get a exception when I send mail Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get a exception when I send mail

    InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
    I have done this in servlet.xml
    Code:
    	<bean id="multipartResolver" 
    	class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    the following is my code

    Code:
    		
    		MultipartHttpServletRequest request = &#40;MultipartHttpServletRequest&#41; rst;
    		String text = request.getParameter&#40;"text"&#41;.trim&#40;&#41;;
    		String sendto = request.getParameter&#40;"sendto"&#41;.trim&#40;&#41;;
    		String subject = request.getParameter&#40;"subject"&#41;.trim&#40;&#41;;
    		MultipartFile f = request.getFile&#40;"attachement"&#41;;
    
    		logger.info&#40;text&#41;;
    		logger.info&#40;sendto&#41;;
    		logger.info&#40;subject&#41;;
    
    		MimeMessage message = this.getJavaMailSender&#40;&#41;.createMimeMessage&#40;&#41;;
    		
    				MimeMessageHelper helper = new MimeMessageHelper&#40;message, true&#41;;
    		helper.setFrom&#40;"[email protected]"&#41;;
    		helper.setTo&#40;sendto&#41;;
    				helper.setText&#40;text&#41;;
    		helper.setSubject&#40;subject&#41;;
    		if&#40;f.getSize&#40;&#41;!=0&#41;&#123;
    			InputStreamResource s = new InputStreamResource&#40;f.getInputStream&#40;&#41;, ""&#41;;
    		 	helper.addAttachment&#40;f.getOriginalFilename&#40;&#41;, s&#41;;
    		&#125;
    
    						try&#123;
    			this.getJavaMailSender&#40;&#41;.send&#40;message&#41;;
    		&#125;catch&#40;Exception e&#41;&#123;
    		   e.printStackTrace&#40;&#41;;
    		&#125;
    		Map model = new HashMap&#40;&#41;;
    
    		return new ModelAndView&#40;new RedirectView&#40;"welcome.sf"&#41;&#41;;
    when I send mail ,I got the Exception .I have looked the source code and I knew that when the InputStreamResource was used once,the stream will be set null. anybody could tell me how can I send mail with attachment normally? and could you paste your code for me?

    thanks in advance! :?:

  • #2
    It might be worthwhile trying to create your own javax.activation.DataSource implementation that will wrap your stream so you can pass it in. If you want to create a JIRA issue for this we can create one and add it to Spring.

    Rob

    Comment


    • #3
      We've created a few different InputStreamSource implementations, that always return a fresh (unread) InputStream, because we got the same problem you're having.

      Also check out org.springframework.core.io.FileSystemResource, which also implements InputStreamSource eventually.

      There's an addAttachment method in org.springframework.mail.javamail.MimeMessageHelpe r that takes an InputStreamSource.

      Comment

      Working...
      X