2015-02-24 87 views
1

我想用Microsoft.Exchange.WebServices.Data发送加密的电子邮件。加密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(); 

    } 

但是还是我得到与Outlook中没有加密的纯电子邮件。我哪里出错了?

回答

0

我在MSDN forum上发布了一条建议。尝试将ItemClass设置为“IPM.Note.SMIME”。