11

I want to send a Pdf as an e-mail attachment (I am using the JavaMail API ). I have the Pdf (generated by jasper) as an byte[].

public InputStream exportPdfToInputStream(User user) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, JRException, IOException{
        JasperPrint jasperPrint = createJasperPrintObject(user);
        byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrint);
        return new ByteArrayInputStream(pdfByteArray);
    }

Here is the code that I am using to construct the MimeBodyPart that will be the attachment:

    if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
        MimeBodyPart attachment = new MimeBodyPart(arrayInputStream);
        attachment.setHeader("Content-Type", "application/pdf");
        mimeMultipart.addBodyPart(attachment);
    }

This code gives me this error:

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.IOException: Error in encoded stream: needed at least 2 valid base64 characters, but only got 1 before padding character (=), the 10 most recent characters were: "\24\163\193\n\185\194\216#\208="
Atticus
  • 1,622
  • 7
  • 32
  • 55

2 Answers2

23

I have found a solution as suggested in this thread. It seems that there is a DataSource class created just for this purpose. Hope this example will help others also.

    if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
        // create the second message part with the attachment from a OutputStrean
        MimeBodyPart attachment= new MimeBodyPart();
        ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf"); 
        attachment.setDataHandler(new DataHandler(ds));
        attachment.setFileName("Report.pdf");
        mimeMultipart.addBodyPart(attachment);
    }
Community
  • 1
  • 1
Atticus
  • 1,622
  • 7
  • 32
  • 55
  • 2
    This will completely read the InputStream and the byte array is completely stored in the ByteArrayDataSource. So you will have high memory usage for large attachments. Right ? – Tama May 05 '20 at 12:54
6

The constructor you used is for parsing a mime part from the transport.

Your second example should work out right. You may consider

  • not to convert to InputStream and back, this will make unnecessary copies
  • add a disposition ( e.g. bp.setDisposition(Part.ATTACHMENT); )
mtraut
  • 4,720
  • 3
  • 24
  • 33
  • @mtraut: You said not to convert it to InputStream and back, but how can I do it without an InputStream? – Atticus Jan 27 '11 at 14:45
  • 1
    use the **ByteArrayDataSource(byte[] data, String type)** constructor and the "pdfByteArray" – mtraut Jan 27 '11 at 15:08
  • @mtraut: Thanks, that is what I have already submitted in my answer. But thanks anyway. I though there is another even simpler way to do it! – Atticus Jan 27 '11 at 15:13
  • @Atticus - not exactly. in your answer you use the INputStream constructor, which results in another copy... – mtraut Jan 27 '11 at 16:46
  • @mtraut: ok, now I got it. One more thing...why is the setDisposition method needed? It works fine without setting it. – Atticus Jan 28 '11 at 08:31
  • 1
    Using "disposition" you can try to control the way that a client handles the part. While this is not honoured correctly by all kind of implementations, "attachment" will most definitely result in the expected behavior. While there's little risk with a PDF attachment, "text/html" or "text/plain" attachments may be rendered **inline** within the email if not declared correctly. Consider it good style... – mtraut Jan 28 '11 at 14:21