【问题标题】:Writing an encrypted mail via Exchange Web Services通过 Exchange Web 服务编写加密邮件
【发布时间】:2015-02-25 08:35:52
【问题描述】:

我想使用 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();

【问题讨论】:

    标签: email service exchange-server web encryption


    【解决方案1】:

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

    使用 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
    

    只需使用 StringWriter 将所有这些放在一起。结果就是您的 MIME 正文。

    【讨论】:

    • 感谢您的回答。我想使用我自己从 LDAP 获得的 X509Certificate2。
    • 由于您需要自己执行整个加密,所以这不是问题。
    • 你能举个例子吗?我知道如何使用 mailMessage 类创建加密的 MailMessage。也许我可以使用这个 Header 并将它们绑定到我的 EMailMessage?
    • 那么您已经有 MIME 格式的加密邮件了?然后继续创建一个 EmailMessage,如示例所示。
    • 是的,我已经有一条 MIME 格式的加密邮件。但这条消息是 MailMessage 而不是 EMailMessage。我不能将 MailMessage 转换为 EMailMessage 对吗?
    猜你喜欢
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多