gpt4 book ai didi

c# - 让 Outlook 识别我的数字签名电子邮件

转载 作者:行者123 更新时间:2023-11-30 14:59:59 25 4
gpt4 key购买 nike

我正在使用 C# .NET 4.0 发送签名的 SMTP 邮件消息,如下所示:

    private void SendMailMessage(object data)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(new MailAddress(emailTo));
message.Subject = "Subject";
message.IsBodyHtml = true;
message.Body += "Blah blah blah.";
byte[] messageBytes = Encoding.ASCII.GetBytes(message.Body);
SignedCms Cms = new SignedCms(new ContentInfo(messageBytes));
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);
Cms.ComputeSignature(Signer);
byte[] SignedBytes = Cms.Encode();
MemoryStream signedStream = new MemoryStream(SignedBytes);
AlternateView signedView = new AlternateView(signedStream, "application/pkcs7-mime; smime-type=signed-data;name=sig.p7m");
message.AlternateViews.Add(signedView);
SmtpClient client = new SmtpClient(smtpServer, int.Parse(smtpServerPort));
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
message.Dispose();
client = null;
}

据我所知,这种“有效”是因为如果查看消息的原始数据,我会看到带有大 PKCS 签名的备用 View 。但 Outlook 无法识别它。 Outlook 客户端通常会识别已签名的消息并尝试验证它们并在消息上放置一个小证书等等......

我想要那个……我错过了什么?

编辑:我自己在这方面取得了一些进展,但仍然遇到一些麻烦。这是代码现在的样子:

    private void SendMailMessage(string emailTo)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(new MailAddress(emailTo));
message.Subject = "Special Delivery";
message.IsBodyHtml = false;
string body = "Content-Type: text/plain;charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\nHere is some body text!";
byte[] messageBytes = Encoding.ASCII.GetBytes(body);
ContentInfo content = new ContentInfo(messageBytes);
SignedCms signedCms = new SignedCms(content, false);
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);
signedCms.ComputeSignature(Signer);
byte[] signedBytes = signedCms.Encode();
MemoryStream ms = new MemoryStream(signedBytes);
AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
message.AlternateViews.Add(av);
SmtpClient client = new SmtpClient(smtpServer, int.Parse(smtpServerPort));
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
message.Dispose();
client = null;
}

值得注意的是,这次我将 message.Body 留空,只发送了 AlternateView。现在,当我将其发送到 Outlook 收件箱时,我的电子邮件上出现了挂锁图标,S/MIME 小工具启动并尝试验证签名者,但失败了。用于签署电子邮件的证书由公开信任的 CA 颁发。 编辑:那是我的错,证书没有“安全电子邮件”使用属性。我会得到一张新证书。

当我将同一封电子邮件发送到 Gmail 地址时,我收到一封空白邮件,其中包含一堆垃圾 *.p7m 附件。

最佳答案

在 Outlook 中实现此功能的代码如下所示:

private void SendMailMessage(string emailTo)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(fromAddress);
message.To.Add(new MailAddress(emailTo));
message.Subject = "Regarding your lottery winnings";
message.IsBodyHtml = false;
string body = "Content-Type: text/plain;charset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\nBlah blah blah blah blah blah.";
byte[] messageBytes = Encoding.ASCII.GetBytes(body);
ContentInfo content = new ContentInfo(messageBytes);
SignedCms signedCms = new SignedCms(content, false);
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, emailCert);
signedCms.ComputeSignature(Signer);
byte[] signedBytes = signedCms.Encode();
MemoryStream ms = new MemoryStream(signedBytes);
AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
message.AlternateViews.Add(av);
SmtpClient client = new SmtpClient(smtpServer, int.Parse(smtpServerPort));
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
message.Dispose();
client = null;
}

当然要使用有效的证书。现在,当我发送这封电子邮件并在 Outlook 中查看时,我在电子邮件上看到了证书图标,并且 S/MIME 控件成功验证了签名,并且文本按照我想要的方式显示,而不向用户显示标题.

请注意,我必须留言。正文为空。将任何内容放入 message.Body 中都会破坏它。

关于c# - 让 Outlook 识别我的数字签名电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15981001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com