title: Create multipart using JavaMail
body: <p>I would like to create a <code>MimeMessage</code> which must have two parts as shown below picture (Part_0 and Part_2) </p>
  
  <p><a href="https://i.stack.imgur.com/uCKIZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uCKIZ.png" alt="example s/mime"></a></p>
  
  <p>I'm trying to use below code to generated s/mime </p>
  
  <pre><code>public static void main(String[] a) throws Exception {
  
      // create some properties and get the default Session
      Properties props = new Properties();
      // props.put("mail.smtp.host", host);
  
      Session session = Session.getInstance(props, null);
      // session.setDebug(debug);
      MimeMessage msg = new MimeMessage(session);
      try {
  
          msg.addHeader("Content-Type", "multipart/signed; protocol=\"application/pkcs7-signature;");
  
          msg.setFrom(new InternetAddress(FROM_EMAIL));
          InternetAddress[] address = {new InternetAddress(
              TO_EMAIL)};
          msg.setRecipients(Message.RecipientType.TO, address);
          msg.setSubject("Test Subject");
          msg.setSentDate(new Date());
  
          // create and fill the first message part
          MimeBodyPart bodyPart1 = new MimeBodyPart();
  
          bodyPart1.setHeader("Content-Type", "text/html; charset=utf-8");
          bodyPart1.setContent("&lt;b&gt;Hello World&lt;/b&gt;", "text/html");
  
          Multipart multiPart = new MimeMultipart();
          multiPart.addBodyPart(bodyPart1, 0);
  
          try (OutputStream str = Files.newOutputStream(Paths
                  .get(UNSIGNED_MIME))) {
              bodyPart1.writeTo(str);
          }
  
          signMime();
  
          MimeBodyPart attachPart = new MimeBodyPart();
  
          String filename = SIGNED_VALUE;
  
          DataSource source = new FileDataSource(filename);
  
          attachPart.setDataHandler(new DataHandler(source));
          attachPart.setFileName("smime.p7s");
          attachPart.addHeader("Content-Type", "application/pkcs7-signature; name=smime.p7s;");
          attachPart.addHeader("Content-Transfer-Encoding", "base64");
          attachPart.addHeader("Content-Description", "S/MIME Cryptographic Signature");
  
          multiPart.addBodyPart(attachPart);
  
          msg.setContent(multiPart, "multipart/signed; protocol=\"application/pkcs7-signature\"; ");
  
          msg.saveChanges();
          try (OutputStream str = Files.newOutputStream(Paths
                  .get(SIGNED_MIME))) {
              msg.writeTo(str);
          }
  
      } catch (MessagingException mex) {
          mex.printStackTrace();
          Exception ex = null;
          if ((ex = mex.getNextException()) != null) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>I used two <code>MimeBodyPart</code> however I always got one Part_0 and generated eml file shown below.</p>
  
  <p><a href="https://i.stack.imgur.com/vt3eI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vt3eI.png" alt="enter image description here"></a></p>