title: Java- Get PDF from byte array from db and e-mail as attachment
body: <p>Good day</p>
  
  <p>I am trying to get the pdf content from a byte array field on the database and then e-mail it as attachment. At the moment, the file itself gets generated, but the attachment is blank. </p>
  
  <p>When I do a part.getContent, the instance of the content is of type String and never of type ByteArrayInputStream. Please assist.</p>
  
  <pre><code>static MimeBodyPart readBodyPart(java.io.ObjectInputStream in) throws IOException {
      MimeBodyPart part = null;
  
      final int sz = in.readInt();
  
      if (sz &gt; 0) {
          byte[] buf = new byte[sz];
          in.readFully(buf);
  
          ByteArrayInputStream bis = new ByteArrayInputStream(buf);
  
          try {
              part = new MimeBodyPart(bis);
  
              final Object content = part.getContent();
  
              if (content instanceof ByteArrayInputStream) {
                  final DataHandler dh = new DataHandler(new ByteArrayDataSource((ByteArrayInputStream)content, part.getContentType()));
                  part.setDataHandler(dh);
              }
          }
          catch (javax.mail.MessagingException e) {
              throw new IOException(e);
          }
          finally {
              bis.close();
          }
      }
  
      return part;
  }
  </code></pre>