使Outlook识别我的数字签名电子邮件
本文关键字:数字签名 电子邮件 我的 识别 Outlook | 更新日期: 2023-09-27 18:08:49
我正在使用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签名的备用视图。但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;
}
值得注意的是我留了言。这次主体空白,只发送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控件成功地验证了签名,并且文本像我想要的那样显示出来,而不向用户显示标题。
请注意,我必须留下信息。身体是空的。传递信息。