title: Send encrypted and signed email using C#
body: <p>I want to send an encrypted and signed mail without using any third-party API.
  If I send only the alternate view with the signature, Windows Mail can validate it. If I send only with alternate view with encrypted data, Windows Mail can decipher it. But if I send both, Windows Mail gets 2 attachements. If I sign the <code>encryptedBytes</code> and add those signed bytes to the alternative view it only validates the signature and the message is empty.
  Any idea?</p>
  
  <pre><code>MailMessage message = new MailMessage();
  message.From = new MailAddress(lblMail.Text);
  message.Subject = txtSubject.Text;
  
  string body = "Content-Type: text/plain\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n" + structForm();
  
  byte[] messageData = Encoding.ASCII.GetBytes(body);
  ContentInfo content = new ContentInfo(messageData);
  EnvelopedCms envelopedCms = new EnvelopedCms(content);
  
  message.To.Add(new MailAddress(provMail));
  
  CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, this.certificate);
  envelopedCms.Encrypt(recipient);
  
  byte[] encryptedBytes = envelopedCms.Encode();
  
  SignedCms Cms = new SignedCms(new ContentInfo(encryptedBytes));
  CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, new X509Certificate2(@"c:\serv.pfx","123"));
  
  Cms.ComputeSignature(Signer);
  byte[] SignedBytes = Cms.Encode();
  
  MemoryStream encryptedStream = new MemoryStream(encryptedBytes);
  AlternateView encryptedView = new AlternateView(encryptedStream, "application/pkcs7-mime; smime-type=signed--data;name=smime.p7m");
  message.AlternateViews.Add(encryptedView);
  MemoryStream signedStream = new MemoryStream(SignedBytes);
  AlternateView signedView = new AlternateView(signedStream, "application/pkcs7-mime; smime-type=signed-data;name=sig.p7m");
  message.AlternateViews.Add(signedView);
  
  
  System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("emailaddress@xpto.com", "XXXXXX");
  SmtpClient client = new SmtpClient("smtp.xpto.com");
  
  client.UseDefaultCredentials = false;
  client.Credentials = SMTPUserInfo;
  
  client.Send(message);
  
  Label2.Text = "Assinado e cifrado!";
  </code></pre>