title: BouncyCastle java - create S/MIME 4 AuthEnvelopedData encrypted e-mail
body: <p>I'd like to send S/MIME 4 encrypted e-mail with <a href="https://www.bouncycastle.org/" rel="nofollow noreferrer">bouncycastle</a>.</p>
  <p>I'm no expert here, but as far as I understood, it boils down to creating <a href="https://www.rfc-editor.org/rfc/rfc8551#section-2.4.4" rel="nofollow noreferrer">AuthEnvelopedData</a> message.</p>
  <p>The MIME message should have following content type: <code>Content-Type: application/pkcs7-mime; smime-type=authEnveloped-data;</code>.</p>
  <p>However, by searching through bouncycastle <a href="https://github.com/bcgit/bc-java/tree/master/mail/src/main/java/org/bouncycastle/mail/smime/examples" rel="nofollow noreferrer">examples</a> and by a full text search for <code>authEnveloped-data</code> string in the source code, I couldn't find a way how to create it.</p>
  <p>I can see that bouncycastle implements <a href="https://github.com/bcgit/bc-java/blob/master/util/src/main/java/org/bouncycastle/asn1/cms/AuthEnvelopedData.java" rel="nofollow noreferrer">AuthEnvelopedData</a> class, but that seems to be for parsing the incoming messages, while I'm looking for something that implements the message creation.</p>
  <p>Note: that sample code <a href="https://github.com/bcgit/bc-java/blob/master/mail/src/main/java/org/bouncycastle/mail/smime/examples/CreateEncryptedMail.java" rel="nofollow noreferrer">here</a> is creating message with content type <code>application/pkcs7-mime; name=&quot;smime.p7m&quot;; smime-type=enveloped-data</code>. Which is the SMIME 3 &quot;less secure&quot; variant.</p>