【问题标题】:.NetCore Signing Emails with Certificate.NetCore 使用证书签署电子邮件
【发布时间】:2021-09-29 11:38:39
【问题描述】:

首先,我尝试了 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);
}

【问题讨论】:

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


【解决方案1】:

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

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

我还需要做一些工作才能对此进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2017-10-11
    • 2016-08-05
    • 2014-05-15
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多