Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3433936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:38:13+00:00 2026-05-18T07:38:13+00:00

I want to send an encrypted and signed mail without using any third-party API.

  • 0

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 encryptedBytes and add those signed bytes to the alternative view it only validates the signature and the message is empty.
Any idea?

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!";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:38:13+00:00Added an answer on May 18, 2026 at 7:38 am

    You should sign first, then encrypt.

    While the original CMS and S/MIME specifications allow you to do the operations in either order, later work pointed out that signing a document that you can’t read is a really bad idea. The signature should be over the plain-text.


    The resulting MIME message should only have a single part, which should be S/MIME enveloped-data. Your message has two parts, and the encrypted part is mis-labeled with a signed-data content-type. Create and sign the SignedCms object. Encode it, and use the encoded value as the content of an EnvelopedCms object. Encrypt that, and use its encoded value as the content of your MailMessage, with a content type of “application/pkcs7-mime; smime-type=enveloped-data”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For testing purposes I want send mail to my localhost user account rather than
If I want to send mail not via SMTP, but rather via sendmail, is
I want so send every week an update by email. But Im afraid that
I want to send email from a Ruby application. Is there a call in
I want to send an array constructed in javascript with the selected values of
I want to send a message to a thread and handle it in the
I want to send some strings in a list in a POST call. eg:
I want to send an Email message after a page is returned to the
I want to send a URI as the value of a query/matrix parameter. Before
I just want to send SMS from my web application in PHP. Can anyone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.