Email 通过Exchange Web服务写入加密邮件

Email 通过Exchange Web服务写入加密邮件,email,service,exchange-server,web,encryption,Email,Service,Exchange Server,Web,Encryption,我想使用C#通过Exchange WEb服务发送加密电子邮件。 有可能吗? 谢谢 编辑: 我的邮件正文加密程序: public static byte[] encry(string body, ContentTyp typ, string to ) { X509Certificate2 cert = GetMailCertificate(to); StringBuilder msg = new StringBuilder();

我想使用C#通过Exchange WEb服务发送加密电子邮件。 有可能吗? 谢谢
编辑:
我的邮件正文加密程序:

        public static byte[] encry(string body, ContentTyp typ, string to )
    {
        X509Certificate2 cert = GetMailCertificate(to);
        StringBuilder msg = new StringBuilder();
        msg.AppendLine(string.Format("Content-Type: text/{0}; charset=\"iso-8859-1\"", typ.ToString()));
        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, cert);
        envelope.Encrypt(recipient);
        //System.IO.MemoryStream ms = new System.IO.MemoryStream(envelope.Encode());
        return envelope.Encode();
    }
主要

 byte [] con = encrypted.encry("test", encrypted.ContentTyp.plain, "test@server.com");
        EmailMessage msg1 = new EmailMessage(_server);
        msg1.MimeContent = new MimeContent("UTF-8", con);
        msg1.ToRecipients.Add("user@server.com");

        msg1.InternetMessageHeaders = ??
        msg1.Send();

如果您指的是S/Mime加密,则必须根据和创建加密消息。完成后,您可以发送消息

使用EWS管理的API,可以按如下方式完成:

var item = new EmailMessage(service);
item.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, content);
// Set recipient infos, etc.
item.Send();
编辑: 您应该添加标准标题,如From、To、Date、Subject等以及内容类型

Subject: Test
From: "sender" <sender@yourcompany.com>
To: "recipient" <recipient@othercompany.com>
Content-Type: application/pkcs7-mime; smime-type=signed-data; name=smime.p7m
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=smime.p7m

Your encrypted body goes here
主题:测试
发件人:“发件人”
收件人:“收件人”
内容类型:应用程序/pkcs7 mime;smime类型=有符号数据;name=smime.p7m
内容传输编码:base64
内容处置:附件;filename=smime.p7m
你的加密身体在这里

只要用一个StringWriter把所有这些放在一起。结果就是你的MIME身体。

谢谢你的回答。我想使用我自己从LDAP获得的X509Certificate2。因为您需要自己执行整个加密,这不是问题。您能给我举个例子吗?我知道如何使用MailMessage类创建加密的MailMessage。也许我可以使用这个标题并将它们绑定到我的电子邮件?那么你已经有MIME格式的加密邮件了?然后继续创建一封电子邮件,如示例所示。是的,我已经有一封MIME格式的加密邮件。但此消息是MailMessage而不是EMailMessage。我无法将MailMessage转换为EMailMessage,对吗?