|
From: Matt S. <sga...@us...> - 2005-03-11 21:37:51
|
This might be overkill, but Spring could provide a
ReusableInputStreamProxy that was passed down to the JavaMail
infrastructure to avoid this limitation. The proxy would keep a copy of
the contents of the InputStream in memory so that it could be accessed
multiple times.
Matt
Juergen Hoeller wrote:
> This is a general problem with JavaMail: It will read any such InputStream
> from a javax.activation.DataSource multiple times. Consequently, a
> DataSource needs to return a fresh InputStream on each getInputStream()
> call. Passing the raw InputStream wouldn't help here; this is a general
> limitation.
>
> Actually, the javadoc of "addAttachment(String attachmentFilename,
> DataSource dataSource)" points this out. I'll add a corresponding warning to
> the addAttachment version that takes an InputStreamSource too.
> InputStreamResource itself recommends to return a fresh stream too, BTW.
>
> I also consider adding a check and corresponding exception to the
> addAttachment version that takes an InputStreamSource: if the passed-in
> object is a core.io.Resource whose isOpen() method returns true, it's
> clearly an invalid argument - we need a Resource that is able to return a
> fresh InputStream every time.
>
> Juergen
>
>
>
> -----Original Message-----
> From: spr...@li...
> [mailto:spr...@li...]On Behalf
> Of Dmitriy Kopylenko
> Sent: Friday, March 11, 2005 8:21 PM
> To: spr...@li...
> Subject: [Springframework-developer] Re: [Springframework-user] Re: Send
> email with attachment
>
>
> After doing some debugging, I think I found a (bug?) in
> MimeMessageHelper.createDatasource(). The method creates an anonymous
> instance of javax activation DataSource with getInputStream() impl
> which calls inputStreamSource.getInputStream(). It all works fine for
> all the InputStreamSources except for InputStreamResource which allows
> to read wrapped InputStream only once, therefore throwing
> IllegalStateException whenever java mail classes e.g. Transport tries to
> call DataSource.getInputStream() multiple times.
>
> Juergen, would it be reasonable to pass a raw InputStream into
> MimeMessageHelper.createDatasource() or as an alternative "disallow"
> usage of InputStreamResource in MimeMessageHelper? I'd rather do the
> former.
>
> Regards,
> Dmitriy.
>
>
> Ken Krebs wrote:
>
>
>>Henry,
>>
>>From what I can see of your code, it looks like you're getting an
>>InputStream to read a file and manipulating the same file with a
>>separate OutputStream, and then resending the original InputStream,
>>which has already been wound to the end, wrapping this InputStream in
>>a resource and sending the resource as the attachment to the email.
>>
>>Your comment implies you want to send the contents of the file that
>>resulted from your manipulation with the OutputStream. It seems to me
>>you should simply close the original InputStream, create a new
>>InputStream after the file has been manipulated, wrap that in the
>>resource and attach the resource.
>>
>>Ken
>>
>>Henry Lu wrote:
>>
>>
>>>Here is the original code:
>>>
>>>outStream = new ByteArrayOutputStream();
>>> act7 = new Actuate7ReportGenerator();
>>>
>>> LOGGER.debug("report - 1");
>>> success = act7.generateReportInPdf(rptName, paramMap,
>>>outStream, jds);
>>>Then I 'd like to send outStream as an attachment to a email.
>>>
>>>-Henry
>>>
>>>Dmitriy Kopylenko wrote:
>>>
>>>
>>>>And what is the body of doFOP() [related to the InputStream passed in]?
>>>>
>>>>Henry Lu wrote:
>>>>
>>>>
>>>>> class MyStreamMail implements MimeMessagePreparator
>>>>> {
>>>>> InputStream m_fp;
>>>>> public MyStreamMail(InputStream fp)
>>>>> {m_fp = fp;}
>>>>>
>>>>> public void prepare(MimeMessage mimeMessage)
>>>>> throws Exception
>>>>> {
>>>>> MimeMessageHelper message = new
>>>>>MimeMessageHelper(mimeMessage, true, "UTF-8");
>>>>> message.setFrom("zh...@um...");
>>>>> message.setTo("zh...@um...");
>>>>> message.setSubject("my test");
>>>>> message.setText("my test text <img src='cid:myLogo'>", true);
>>>>> message.addInline("myLogo", new
>>>>>ClassPathResource("yellowkey.gif"));
>>>>>
>>>>> message.addAttachment("00.pdf", new
>>>>>InputStreamResource(m_fp));
>>>>> }
>>>>> }
>>>>>
>>>>> ByteArrayInputStream st = new ByteArrayInputStream();
>>>>> doFOP(input_file, st);
>>>>>
>>>>> MyStreamMail mmsg = new MyStreamMail(st);
>>>>> try {
>>>>> mailSender.send(mmsg);
>>>>> } catch (MailException ex)
>>>>> { System.out.println(ex.getMessage()); }
>>>>>
>>>>>-Henry
>>>>>
>>>>>Dmitriy Kopylenko wrote:
>>>>>
>>>>>
>>>>>>So, would you please show the entire snippet of code where you
>>>>>>obtain the InputStream
>>>>>>
>>>>>>Henry Lu wrote:
>>>>>>
>>>>>>
>>>>>>>Mr. Kopylenko, thanks for your quick response. I don't have an
>>>>>>>existing file for an attachement. All I have is a inputStream of
>>>>>>>output from another function.
>>>>>>>
>>>>>>>-Henry
>>>>>>>
>>>>>>>Dmitriy Kopylenko wrote:
>>>>>>>
>>>>>>>
>>>>>>>>Henry,
>>>>>>>>
>>>>>>>>how do you obtain the InputStream "inputStream" in your code,
>>>>>>>>before you try to wrap it with InputStreamResource?
>>>>>>>>
>>>>>>>>You could use FileSystemResource instead i.e.
>>>>>>>>helper.addAttachment("myattachmentname.extension_goes_here", new
>>>>>>>>FileSystemResource(new
>>>>>>>>File("path_to_the_file/myattachmentname.extension_goes_here"));
>>>>>>>>
>>>>>>>>Dmitriy.
>>>>>>>>
>>>>>>>>
>>>>>>>>Henry Lu wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>>Could you show me a simple working example of this?
>>>>>>>>>
>>>>>>>>>-Henry
>>>>>>>>>
>>>>>>>>>Dmitriy Kopylenko wrote:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>>InputStreamResource could only wrap the underlying InputSrteam
>>>>>>>>>>if it hasn't been read multiple times.
>>>>>>>>>>
>>>>>>>>>>Dmitriy.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>Henry Lu wrote:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>>Is there anyone who can help me why I got the following
>>>>>>>>>>>errors when I ran the code:
>>>>>>>>>>>
>>>>>>>>>>>MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
>>>>>>>>>>>true);
>>>>>>>>>>>helper.addAttachment("myattachmentname.extension_goes_here",
>>>>>>>>>>>new InputStreamResource(inputStream));
>>>>>>>>>>>
>>>>>>>>>>>ERROR:
>>>>>>>>>>> [java] Could not prepare mail: InputStream has already
>>>>>>>>>>>been read - do not u
>>>>>>>>>>>se InputStreamResource if a stream needs to be read multiple
>>>>>>>>>>>times; nested excep
>>>>>>>>>>>tion is java.lang.IllegalStateException: InputStream has
>>>>>>>>>>>already been read - do
>>>>>>>>>>>not use InputStreamResource if a stream needs to be read
>>>>>>>>>>>multiple times
>>>>>>>>>>>
>>>>>>>>>>>-Henry
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>Matt Sgarlata wrote:
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>>MimeMessageHelper helper = new
>>>>>>>>>>>>MimeMessageHelper(mimeMessage, true);
>>>>>>>>>>>>helper.addAttachment("myattachmentname.extension_goes_here",
>>>>>>>>>>>>new InputStreamResource(inputStream));
>>>>>>>>>>>>
>>>>>>>>>>>>The rest of the code you need is in the reference
>>>>>>>>>>>>documentation.
>>>>>>>>>>>>
>>>>>>>>>>>>Matt
>>>>>>>>>>>>
>>>>>>>>>>>>Henry Lu wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>>Can someone show me an example how to send an email with an
>>>>>>>>>>>>>attachment with stream instead of file name of File object
>>>>>>>>>>>>>in Spring package?
>>>>>>>>>>>>>
>>>>>>>>>>>>>-Henry
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>>-------------------------------------------------------
>>>>>>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>>>>>>Read honest & candid reviews on hundreds of IT Products
>>>>>>>>>>>>>from real users.
>>>>>>>>>>>>>Discover which products truly live up to the hype. Start
>>>>>>>>>>>>>reading now.
>>>>>>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>-------------------------------------------------------
>>>>>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>>>>>Read honest & candid reviews on hundreds of IT Products from
>>>>>>>>>>>>real users.
>>>>>>>>>>>>Discover which products truly live up to the hype. Start
>>>>>>>>>>>>reading now.
>>>>>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>>>>>_______________________________________________
>>>>>>>>>>>>Springframework-user mailing list
>>>>>>>>>>>>Spr...@li...
>>>>>>>>>>>>
>
> https://lists.sourceforge.net/lists/listinfo/springframework-user
>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>>-------------------------------------------------------
>>>>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>>>>Read honest & candid reviews on hundreds of IT Products from
>>>>>>>>>>>real users.
>>>>>>>>>>>Discover which products truly live up to the hype. Start
>>>>>>>>>>>reading now.
>>>>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>>>>_______________________________________________
>>>>>>>>>>>Springframework-user mailing list
>>>>>>>>>>>Spr...@li...
>>>>>>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>-------------------------------------------------------
>>>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>>>Read honest & candid reviews on hundreds of IT Products from
>>>>>>>>>>real users.
>>>>>>>>>>Discover which products truly live up to the hype. Start
>>>>>>>>>>reading now.
>>>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>>>_______________________________________________
>>>>>>>>>>Springframework-user mailing list
>>>>>>>>>>Spr...@li...
>>>>>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>-------------------------------------------------------
>>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>>Read honest & candid reviews on hundreds of IT Products from
>>>>>>>>>real users.
>>>>>>>>>Discover which products truly live up to the hype. Start
>>>>>>>>>reading now.
>>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>>_______________________________________________
>>>>>>>>>Springframework-user mailing list
>>>>>>>>>Spr...@li...
>>>>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>-------------------------------------------------------
>>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>>Read honest & candid reviews on hundreds of IT Products from
>>>>>>>>real users.
>>>>>>>>Discover which products truly live up to the hype. Start reading
>>>>>>>>now.
>>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>>_______________________________________________
>>>>>>>>Springframework-user mailing list
>>>>>>>>Spr...@li...
>>>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>-------------------------------------------------------
>>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>>Read honest & candid reviews on hundreds of IT Products from real
>>>>>>>users.
>>>>>>>Discover which products truly live up to the hype. Start reading
>>>>>>>now.
>>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>>_______________________________________________
>>>>>>>Springframework-user mailing list
>>>>>>>Spr...@li...
>>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>-------------------------------------------------------
>>>>>>SF email is sponsored by - The IT Product Guide
>>>>>>Read honest & candid reviews on hundreds of IT Products from real
>>>>>>users.
>>>>>>Discover which products truly live up to the hype. Start reading now.
>>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>>_______________________________________________
>>>>>>Springframework-user mailing list
>>>>>>Spr...@li...
>>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>>-------------------------------------------------------
>>>>>SF email is sponsored by - The IT Product Guide
>>>>>Read honest & candid reviews on hundreds of IT Products from real
>>>>>users.
>>>>>Discover which products truly live up to the hype. Start reading now.
>>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>>_______________________________________________
>>>>>Springframework-user mailing list
>>>>>Spr...@li...
>>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>-------------------------------------------------------
>>>>SF email is sponsored by - The IT Product Guide
>>>>Read honest & candid reviews on hundreds of IT Products from real
>>>>users.
>>>>Discover which products truly live up to the hype. Start reading now.
>>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>>_______________________________________________
>>>>Springframework-user mailing list
>>>>Spr...@li...
>>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>>
>>>>
>>>
>>>
>>>-------------------------------------------------------
>>>SF email is sponsored by - The IT Product Guide
>>>Read honest & candid reviews on hundreds of IT Products from real users.
>>>Discover which products truly live up to the hype. Start reading now.
>>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>>_______________________________________________
>>>Springframework-user mailing list
>>>Spr...@li...
>>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>>>
>>>
>>
>>
>>
>>
>>-------------------------------------------------------
>>SF email is sponsored by - The IT Product Guide
>>Read honest & candid reviews on hundreds of IT Products from real users.
>>Discover which products truly live up to the hype. Start reading now.
>>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
>>_______________________________________________
>>Springframework-user mailing list
>>Spr...@li...
>>https://lists.sourceforge.net/lists/listinfo/springframework-user
>
>
>
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
> _______________________________________________
> Springframework-developer mailing list
> Spr...@li...
> https://lists.sourceforge.net/lists/listinfo/springframework-developer
>
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real users.
> Discover which products truly live up to the hype. Start reading now.
> http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
|