通过 Exchange Web 服务编写加密邮件

发布于 2024-12-07 03:33:27 字数 1473 浏览 0 评论 0原文

我想使用 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, "[email protected]");
        EmailMessage msg1 = new EmailMessage(_server);
        msg1.MimeContent = new MimeContent("UTF-8", con);
        msg1.ToRecipients.Add("[email protected]");

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

I would like to send an encrypted EMail Message with Exchange WEb Services using C#.
Is there any possibillity?
Thanks

Edit:
My Mail body encrypter:

        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();
    }

Main

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2024-12-14 03:33:27

如果您指的是 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();

编辑:
您应该添加标准标题,例如“发件人”、“收件人”、“日期”、“主题”等以及内容类型。

Subject: Test
From: "sender" <[email protected]>
To: "recipient" <[email protected]>
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 主体。

If you are referring to S/Mime encryption, then you'll have to create the encrypted message according to RFC 3852 and RFC 4134. After you've done that, you can send the message.

Using the EWS Managed API, this can be done as follows:

var item = new EmailMessage(service);
item.MimeContent = new MimeContent(Encoding.ASCII.HeaderName, content);
// Set recipient infos, etc.
item.Send();

EDIT:
You should add the standard headers like From, To, Date, Subject, etc. And the content-type.

Subject: Test
From: "sender" <[email protected]>
To: "recipient" <[email protected]>
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

Just use a StringWriter put all that together. The result is your MIME body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文