来自 .NET 的多部分 HTML 和嵌入图像无需磁盘访问?
我正在开发一种产品,该产品通过生成 HTML 文档来创建 MS Word 输出,其中嵌入了适当的 Word CSS 和 XML 魔法。我现在需要在文档中嵌入 RTF(图像和样式文本),因此正在研究 .NET 中的多部分 HTML 生成功能。
我想通过将 RTF 片段转换为内存中的图像来生成 MHTML 文档,然后将 Base64 编码的数据直接嵌入到 MHTML 对象中。到目前为止,我找不到任何 .NET 解决方案来做到这一点。
MS CDO 的东西很有前途,但它看起来创建附件的唯一方法是提供数据的 URL 并使用 AddRelatedBodyPart() 将其添加到对象中。
System.Net.Mail 包看起来也很引人注目(如文档所述 这里和上一个问题此处),但我没有看到将生成的邮件对象转换为字符串的直接方法 - API 似乎专注于发送邮件消息,而不是生成文件。
现在,我正在推进一种使用 CDO 和临时目录的方法,但我对此作为一种长期方法并不满意。还有更好的建议吗?
I'm working on a product that creates MS Word output by generating an HTML document with the appropriate bits of Word CSS and XML magic embedded in it. I now need to embed RTF (images and styled text) in the document, and so am looking at the multipart HTML generation features in .NET.
I'd like to generate the MHTML document by converting the RTF snippets into images in memory, and then just embedding the Base64-encoded data directly into the MHTML object. So far, I can't find any .NET solutions to do that.
The MS CDO stuff is promising, but it looks like the only way to create an attachment is to provide a URL to the data and use AddRelatedBodyPart() to add it to the object.
The System.Net.Mail package also looks compelling (as documented here and in a previous question here), but I don't see a straightforward way to convert the generated mail object to a string -- the API seems to be focused around sending mail messages, not generating files.
Right now, I'm pushing forward with an approach that uses CDO and temporary directories, but I'm unhappy with that as a long-term approach. Any better suggestions?
对于 System.Net.Mail 将其输出发送到用户定义的某种流的能力并没有太多的需求,但它已经被讨论过。您能给我更多关于您的场景的信息吗?我想知道人们如何使用它来证明添加此功能的合理性。
我认为您可以在发送消息之前使用反射将输出流从 FileStream 重定向到其他流。 System.Net.Mail 中的大部分代码都在 Stream 对象周围使用装饰器,因此我怀疑这是可能的,但我对反射的了解还不足以为您提供有关如何执行此操作的良好代码片段。此外,System.Net.Mail 使用自己的 base64 编码器,因此速度非常快。我还对 .Net 4.0 进行了一些调整,因此您可能会从中获得更好的性能,尽管我怀疑它是否会引人注目。
There hasn't been a lot of demand for the ability for System.Net.Mail to send its output to some sort of stream defined by the user but it's been talked about. Can you give me more info about your scenario? I'd like to know how people are using this in terms of justification for adding this feature.
I think you could use reflection to redirect the output stream from a FileStream to some other stream prior to sending the message. Most of the code in System.Net.Mail uses decorators around Stream objects so I suspect that it would be possible but I don't know reflection well enough to give you a good code snippet on how to do this. Also, System.Net.Mail uses its own base64 encoder so it's plenty fast. I tweaked a few things for .Net 4.0 also so you'll probably get even better performance from it although I doubt it would be noticable.