title: byte array to MimeMultipart
body: <p>I'm writing a type of encoder/decoder for MimiMultipart.<br>
  Encoder: MimiMultipart -> HexString<br>
  Decoder: HexString -> MimiMultipart  </p>
  
  <p>This method creates MimeMultipart for later encoding:  </p>
  
  <pre><code>private MimeMultipart compose_mail(String to, String body) throws Exception{
      Document d = XMLConnector.get("body.html");
      String html_body = d.html();
  
      MimeMultipart multipart = new MimeMultipart("related");
  
      // Body part
      BodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setContent(html_body, "text/html; charset=\"UTF-8\"");
      multipart.addBodyPart(messageBodyPart);
  
      // Image part
      File img_file = new File("img/logo.png");
  
      MimeBodyPart messageImgPart = new MimeBodyPart();
      messageImgPart.setHeader("Content-Type", "image/jpeg");
      messageImgPart.setHeader("Content-ID","&lt;logo&gt;");
      messageImgPart.setDisposition(MimeBodyPart.INLINE);
      messageImgPart.setFileName("logo inline image");
      messageImgPart.attachFile(img_file);
      multipart.addBodyPart(messageImgPart);
  
      return MimeMultipart;
  }
  </code></pre>
  
  <p>This method encodes MimeMultipart to HexString: 
  (It also attaches content type with boundary.)</p>
  
  <pre><code>public String encode(MimeMultipart body) throws Exception {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      body.writeTo(baos);
      String hex_body = Hex.encodeHexString(baos.toByteArray());
      hex_body += ";" + body.getContentType();
      return hex_body;
  }
  </code></pre>
  
  <p>The last method has to decode the string:  </p>
  
  <pre><code>public MimeMultipart decode(String encoded_body) throws Exceptions {
      MimeMultipart body = null;
      String[] parts = encoded_body.split(";", 2); 
      byte[] body_b = Hex.decodeHex(parts[0].toCharArray());
      ByteArrayDataSource bads = new ByteArrayDataSource(body_b, parts[1]);
      body = new MimeMultipart(bads);
      return body;  
  }
  </code></pre>
  
  <p>Some checks I did:<br>
  1) I sent original MimeMultipart:</p>
  
  <pre><code>MimeMessage msg = new MimeMessage(session);
  msg.setHeader("Content-Type", "text/html; charset=\"UTF-8\"");
  msg.setSubject(subject, "UTF-8");
  msg.setContent(body, "text/html; charset=\"UTF-8\"");
  Transport.send(msg);
  </code></pre>
  
  <p>I got the mail as needed (html visible as html, image as image).<br>
  When I sent encoded->decoded mail same way, I got source html instead rendered one, and image seems broken.<br>
  2) I checked decoded MimeMultipart and saw 0 parts.  </p>
  
  <p>What may be wrong here?</p>
  
  <p>UPDATE:
  About 2: MimeMultipart contains 0 parts, because parsing from stream is done only on demand, ie during getParts() call. </p>