.NetCore 用证书签名电子邮件

时间:2021-07-22 07:12:37

标签: c# .net-core certificate signing mime-mail

首先,我尝试了 FluentEmail,但我的 .NetCore 版本似乎无法使用它。

我正在创建这样的电子邮件:

var email = new MimeMessage();

// Put all the data in the email

X509Certificate2 cert = new X509Certificate2("fileName");

问题是,我找不到任何有关如何实际执行签名的示例。我能找到的最好的方法是签名和加密,但电子邮件不得加密。这一点真的很重要。

现在有人知道如何签署电子邮件(包括发件人、主题、正文等)。任何帮助将不胜感激。

我可能应该补充一点,证书将位于某个文件路径中,并且该程序将部署在 Linux Docker 容器上。所以代码不能只在 Windows 上工作。

对不起,我应该把明显的东西包起来。这是我第一次尝试通过代码发送电子邮件。

更新 0: 请注意这是否有效,因为 WindowsSecureMimeContext(..)。 (我知道我只在下面截取的代码中签署了正文)。

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

更新 1: 我认为这应该有效:

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

1 个答案:

答案 0 :(得分:0)

我认为这应该是我的问题的答案(示例仅在正文中签名):

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

我还需要做一些工作才能测试这个。