tags:

views:

1037

answers:

3

Hai to every one.

i have report in my jsp page.And i am writting thet report in PDF Format. And i want to send the PDF as E-Mail with attachment,But i dont want store the file in local machine or server,But i want to send an email with attachment..

+1  A: 

You have to write your own implementation of javax.activation.DataSource to read the attachment data from an memory instead of using one of the included implementations (to read from a file, a URL, etc.). If you have the PDF report in a byte array, you can implement a DataSource which returns the byte array wrapped in a ByteArrayOutputStream.

jarnbjo
+1  A: 

If you use Spring's JavaMail API, you can do this sort of thing fairly easily (or at least, as easily as the JavaMail API allows, which isn't much). So you could write something like this:

JavaMailSenderImpl mailSender = ... instantiate and configure JavaMailSenderImpl here
final byte[] data = .... this holds my PDF data

mailSender.send(new MimeMessagePreparator() {
   public void prepare(MimeMessage mimeMessage) throws Exception {
      MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
     // set from, to, subject using helper
     helper.addAttachment("my.pdf", new ByteArrayResource(data));
   } 
});

The attachment data can be any of Spring's Resource abstractions, ByteArrayResource is just one of them.

Note that this part of the Spring API stands on its own, it does not require (but does benefit from) the Spring container.

skaffman
+1  A: 

Hi

Since JavaMail 1.4 - mail.jar - contains javax.mail.util.ByteArrayDataSource

regards

dariusz.czyrnek
Thanks, this was precisely the info I needed.
kg