Encrypting email in Microsoft.Exchange.WebServices.Dat a

I am trying to send an encrypted email using Microsoft.Exchange

public void SendMessage(FacadeModel.EmailMessage message)
    {
        var item = new EWS.EmailMessage(_mailService);

        var handler = new SecureMimeMessageHandler();
        byte[] con = handler.encry("test", "me@mail.com.au");

        item.MimeContent = new EWS.MimeContent(Encoding.ASCII.HeaderName, con);
        item.ToRecipients.Add("me@mail.com.au");
        item.From = new EWS.EmailAddress("", "me@mail.com.au");
        item.Body = "test";
        item.Send();

}

public byte[] encry(string body, string to)
    {
        var store = new X509Store(StoreLocation.LocalMachine);
                store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
                X509Certificate2Collection certs = store.Certificates;

                X509Certificate2 cert1 = GetMatchingCertificate(certs[1], "me@mail.com.au", X509KeyUsageFlags.KeyEncipherment);

        StringBuilder msg = new StringBuilder();
        msg.AppendLine(string.Format("Content-Type: application/pkcs7-mime; smime-type=signed-data;name=smime.p7m"));
        msg.AppendLine("Content-Transfer-Encoding: 7bit");
        msg.AppendLine();
        msg.AppendLine(body);
        EnvelopedCms envelope = new EnvelopedCms(new ContentInfo(Encoding.UTF8.GetBytes(msg.ToString())));
        CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cert1);
        envelope.Encrypt(recipient);

        return envelope.Encode();

    }

But still i am getting a plain email with no encryption in outlook. where have i gone wrong?


  • Edited by nipu7 Wednesday, February 25, 2015 3:14 AM
February 25th, 2015 6:09am

I don't have a sample of doing this, but one thing that looks like it's missing is setting the ItemClass. You might try:

item.ItemClass = "IPM.Note.SMIME";
Let me know how that goes!

Free Windows Admin Tool Kit Click here and download it now
February 27th, 2015 4:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics