来自 .NET 的多部分 HTML 和嵌入图像无需磁盘访问?

发布于 2024-08-02 09:43:55 字数 946 浏览 6 评论 0原文

我正在开发一种产品,该产品通过生成 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?

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

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

发布评论

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

评论(2

偷得浮生 2024-08-09 09:43:55

对于 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.

冷清清 2024-08-09 09:43:55

.NET 中的 Base64 编码支持速度不是很快,而且 System.Net.Mail 也不是通用的。我最终使用了 CHilkat S/MIME 并且效果很好。

The Base64 encoding support in .NET is not very fast, and System.Net.Mail is not general-purpose. I wound up using CHilkat S/MIME and it worked fine.

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