Как подписать, зашифровать и отправить сообщение, используя API веб-службы обмена (ews) с S / MIME - PullRequest
0 голосов
/ 07 сентября 2018

Я следовал этой статье для подписывающего сообщения.при чтении сообщения получателем с помощью клиента 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

...