title: How to extract attachment from application/pkcs7-mime received from Microsoft Graph API in .NET Core
body: <p>I am downloading attachments with Microsoft Graph API in .NET Core 3.1.</p>
  
  <pre><code>await client.Users[mail.OwnerEmail].Messages[mail.Id].Attachments.Request().GetAsync()
  </code></pre>
  
  <p>Recently one email had strange attachment with content type <code>application/pkcs7-mime</code> (extension .p7m). I need to extract attachments inside this signed attachment. Outlook shows 2 attachments for this mail: <code>pdf</code> and <code>xml</code>. I need these 2 files.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/51961935/getting-attachments-of-a-s-mime-signed-mail/56127763#56127763">this</a> thread, but I don't understand the answer.</p>
  
  <p>I tried:</p>
  
  <pre><code>var signed = new SignedCms();
  signed.Decode(signedAttachment.Content);   //Exception: ASN1 corrupted data.
  </code></pre>
  
  <p>and</p>
  
  <pre><code>new SignedCms(new ContentInfo(signedAttachment.Content))
  </code></pre>
  
  <p>and receive some friendly name and version:</p>
  
  <p><a href="https://i.stack.imgur.com/i52JA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i52JA.png" alt="debugger screenshot"></a></p>
  
  <p>but I don't know how to extract attachments.</p>
  
  <p>Edited:</p>
  
  <p>Also tried:</p>
  
  <pre><code>var envelopedCms = new EnvelopedCms();
  envelopedCms.Decode(signedAttachment.Content);
  </code></pre>
  
  <p>and <code>Decrypt</code> ... Nothing.</p>