user2530784
user2530784

Reputation: 21

Encrypting email in Microsoft.Exchange.WebServices.Data

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

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

        var handler = new SecureMimeMessageHandler();
        byte[] con = handler.encry("test", "[email protected]");

        item.MimeContent = new EWS.MimeContent(Encoding.ASCII.HeaderName, con);
        item.ToRecipients.Add("[email protected]");
        item.From = new EWS.EmailAddress("", "[email protected]");
        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], "[email protected]", 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?

Upvotes: 2

Views: 848

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17692

I posted a suggestion on the MSDN forum. Try setting the ItemClass to "IPM.Note.SMIME".

Upvotes: 1

Related Questions