EWS:从签名的电子邮件中检索附件 [英] EWS: Retrieving attachments from signed emails

查看:228
本文介绍了EWS:从签名的电子邮件中检索附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序通过检索附件并将电子邮件分类到子文件夹来管理资源邮箱。最近,客户希望向我们发送签名的电子邮件发送一个问题,因此当程序检索其附件时,将保存名为smime.p7m的文件,而不是文件附件。在Outlook中查看电子邮件时,只有我们想要的附件,此文件不存在。但是,当逐步执行代码时,Email对象中列出的附件仅包含此 .p7m 文件。



我已经从电子邮件中检索到mime内容,但它只是字节。当我在文本编辑器中查看.p7m文件时,我会看到我想要的文件的内容(最终的逗点)!如何获取原始附件,而无需解析感兴趣的内容的.p7m文件?



交换服务器是2010 SP2,这一切都是通过使用EWS Managed API的C#程序。

解决方案

您可以使用 EnvelopedCMS / a>类从加密附件获取MIME。假设您的安全上下文可以访问密钥。

  byte [] content = ...来自smime的字节[]。 p7m attachment ... 
var encrypted = new EnvelopedCms();
encrypted.Decode(content);
encrypted.Decrypt();
byte [] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content;

这将允许您获取未加密的MIME实体(原始电子邮件,无传输标头)。 p>

注意如果邮件已签名,则解密的MIME实体将是另一个附件,其中包含 SMIME Type 标题等于 signed-data 。您可以使用 SignedCMS 类,以公开其内容。应该省略 Decrypt 调用。



然后,您必须解析/解码MIME以提取其正文和附件。



执行此操作的代码显然位于 System.Net.Mime 命名空间,但由于任何原因Microsoft,不提供公开的入场点。我已经在其他地方阅读过您可以使用反射来访问它。这样的缺点是,它不支持,非公共接口在后续版本的框架中会发生变化。 此问题中的代码显示了如何处理 quoted-printable 传输编码。



或者,您可以像我一样编写或借用自己的MIME解析器。不幸的是,由于IP,我无法给你代码。



当时我找不到一个简单的选择。现在我会试着尝试下面链接的NuGet软件包,并给自己留下一些痛苦。从 OpenPOP.Net 开始。






您可以使用此项目中的代码灵感,查看这个问题,或尝试在NuGet上这些软件包。 p>

I have a C# program that manages a resource mailbox by retrieving attachments and categorizing emails into sub-folders. An issue came up recently where the client wishes to send us signed emails, so when the program retrieves their attachments a file named "smime.p7m" is saved instead of the file attachments. This file is not present when reviewing the email in Outlook, only the attachments we want. However, when stepping through the code, the attachments listed in the Email object only contains this .p7m file.

I have retrieved the mime content from the email, but it's just bytes. When I look at the .p7m file in a text editor, I see the contents of the file(s) I want at the bottom file (the ultimate tease)! How do I get the original attachments without having to parse the .p7m file for the content of interest?

The exchange server is 2010 SP2, and this is all happening via a C# program utilizing the EWS Managed API.

解决方案

You can use the EnvelopedCMS class to get the MIME from the encrypted attachment. Assuming your security context has access to the key.

byte[] content = ...The byte[] from the smime.p7m attachment ...
var encrypted = new EnvelopedCms();
encrypted.Decode(content);
encrypted.Decrypt();
byte[] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content;

This will allow you to get the unencrypted MIME Entity (the original email without transport headers).

Note if the message is signed, the decrypted MIME entity will be another single attachment with an SMIME Type Header equal to signed-data. You can repeat the process above using the SignedCMS class, to expose its content. The Decrypt call should be omitted.

You then have to parse/decode the MIME to extract its body and attachments.

The code to do this obviously resides the System.Net.Mime namespace but Microsoft, for whatever reason, offer no public entry point to it. I have read elsewhere than you can use reflection to access it. This disadvantages of this is, its not supported and the non-public interface is subject to change in later versions of the framework. The code in this question shows you how to deal with a quoted-printable Transfer Encoding.

Alternatively, you can write or borrow your own MIME parser, as I did. Unfortunately, I can't give you the code because of IP.

At the time I was unable to find a simple alternative. Now I'd be tempted to try out the NuGet packages linked below and save myself some pain. Starting with OpenPOP.Net.


You could use the code in this project for inspiration, look at the third-party options in this question, or try these packages on NuGet.

这篇关于EWS:从签名的电子邮件中检索附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆