简体   繁体   中英

.NetCore Signing Emails with Certificate

First of all, I tried FluentEmail but that one seems to be broken with my version of .NetCore.

I am creating an email like this:

var email = new MimeMessage();

// Put all the data in the email

X509Certificate2 cert = new X509Certificate2("fileName");

The problem is, I can't find any example of how to actually perform the signing. The best I can find is signing and encrypting but the email MUST NOT be encrypted. That one is really important.

Does anyone now how to sign an email (including sender, subject, body, etc.). Any help would be very much appreciated.

I probably should add that the certificate will be located at some file path and that the program will be deployed on a Linux Docker container. So the code must not only work on Windows.

And sorry, should I have overlocked the obvious. This is the first time I am trying to send an email via code.

Update 0: Note quite sure if this works because of WindowsSecureMimeContext(..) . (I am aware that I only sign the body in the code snipped below).

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new WindowsSecureMimeContext(StoreLocation.LocalMachine))
{
   email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

Update 1: I think this should work:

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new DefaultSecureMimeContext())
{
   email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

I think this should be the answer to my question (the example only signs the body):

X509Certificate2 cert = new X509Certificate2("fileName");
using (var context = new DefaultSecureMimeContext())
{
  email.Body = MultipartSigned.Create(context, new CmsSigner(cert), email.Body);
}

I still need to do some work before I will be able to test this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM