如何使用带有S / MIME的Exchange Web Service(ews)api签名,加密和发送消息

时间:2018-09-07 09:30:21

标签: c# outlook exchangewebservices ews-managed-api smime

我跟随这个article来查看签名消息。 使用Outlook桌面客户端阅读收件人的邮件时,它不带有标记。但是加密图标显示在Outlook中并带有解密的消息

X509Certificate2 signingCertificate = null;
try
{
    signingCertificate = new X509Certificate2(textBoxSenderCertificate.Text, textBoxSenderCertPassword.Text);
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(String.Format("Error when loading signing/encrypting certificate:{0}{1}", Environment.NewLine, ex.Message), "Invalid certificate", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

// Sanity checks ok
buttonSend.Enabled = false;
// Now we attempt to create the signed/encrypted message
// Create the message as normal, then save it to Drafts

var mail = new EmailMessage(_service);
mail.ItemClass = "IPM.Note.SMIME";
var msgId = Guid.NewGuid().ToString("N");
var boundary = "----=_NextPart_" + msgId + "." + (new Random(100)).Next(999999);
var sb = new StringBuilder();
sb.AppendLine("From: aaa@aaa.com");
sb.AppendLine("To: " + textBoxRecipientEmail.Text);
sb.AppendLine("Subject: SMIME test subject " + (new Random(100)).Next(999));
sb.AppendLine("Message-ID:\n <" + msgId + "@aaa.onmicrosoft.com>");
sb.AppendLine("Content-Language: en-US");
sb.AppendLine("Content-Type: application/pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\"");
sb.AppendLine("Content-Disposition: attachment; filename=\"smime.p7m\"");
sb.AppendLine("Content-Transfer-Encoding: base64");
sb.AppendLine("MIME-Version: 1.0");
sb.AppendLine("");
var mimeHeader = sb.ToString();
sb.Clear();
sb.AppendLine("Content-Type: multipart/alternative;");
sb.AppendLine("\tboundary=\"" + boundary + "\"");
sb.AppendLine("");
sb.AppendLine("This is a multipart message in MIME format.");
sb.AppendLine("");
sb.AppendLine("--" + boundary);
sb.AppendLine("Content-Type: text/plain;");
sb.AppendLine("\tcharset=\"us-ascii\"");
sb.AppendLine("Content-Transfer-Encoding: 7bit");
sb.AppendLine("");
sb.AppendLine("Hello!");
sb.AppendLine("I am manually generated signed message.2");
sb.AppendLine("");
sb.AppendLine("");
sb.AppendLine("--" + boundary);
sb.AppendLine("Content-Type: text/html;");
sb.AppendLine("\tcharset=\"us-ascii\"");
sb.AppendLine("Content-Transfer-Encoding: quoted-printable");
sb.AppendLine("");
sb.AppendLine("<html><head/><body><p>Hello!</p><p>I am manually generated signed message4.</p></body></html>");
sb.AppendLine("");
sb.AppendLine("--" + boundary);
var bodyBytes = Encoding.ASCII.GetBytes(sb.ToString());
var content = new ContentInfo(bodyBytes);
SignedCms signedCms = new SignedCms(content, false);
CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, signingCertificate);
signedCms.ComputeSignature(signer);
var bodySignedBytes = signedCms.Encode();

EnvelopedCms Envelope = new EnvelopedCms(new ContentInfo(bodySignedBytes));
CmsRecipient Recipient = new CmsRecipient(
    SubjectIdentifierType.IssuerAndSerialNumber, signingCertificateEncrypt);
Envelope.Encrypt(Recipient);
byte[] EncryptedBytes = Envelope.Encode();
mimeHeader += Convert.ToBase64String(EncryptedBytes);

mail.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, Encoding.ASCII.GetBytes(mimeHeader));
mail.Send();

在这里,我也使用符号字节数组进行加密,并且不确定这是实现它的正确方法。 enter image description here

0 个答案:

没有答案