title: Send e-mail in Pdf attachment as stream
body: <p>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 <code>byte[]</code>.</p>
  
  <pre><code>public InputStream exportPdfToInputStream(User user) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, JRException, IOException{
          JasperPrint jasperPrint = createJasperPrintObject(user);
          byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrint);
          return new ByteArrayInputStream(pdfByteArray);
      }
  </code></pre>
  
  <p>Here is the code that I am using to construct the <code>MimeBodyPart</code> that will be the attachment:</p>
  
  <pre><code>    if (arrayInputStream != null &amp;&amp; arrayInputStream instanceof ByteArrayInputStream) {
          MimeBodyPart attachment = new MimeBodyPart(arrayInputStream);
          attachment.setHeader("Content-Type", "application/pdf");
          mimeMultipart.addBodyPart(attachment);
      }
  </code></pre>
  
  <p>This code gives me this error:</p>
  
  <pre><code>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="
  </code></pre>