views:

332

answers:

2

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?

A: 

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.

Steven Sudit
Actually System.Net.Mail uses its own base64 encoding and it's plenty fast according to our perf tests. It's also fairly general purpose but I admit that there are some shortcomings around it.
Jeff Tucker
Jeff, I just double-checked the implementation of Convert.ToBase64String, and it looks like you're correct: it properly unrolls the inner loop, so it should be fast. The odd thing is that I remember looking at a native .NET Base64 conversion method that didn't do this, but I don't know where. Is this early-onset Altzheimers or can you think of the where it might be?
Steven Sudit
+1  A: 

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.

Jeff Tucker
Background: I need to generate Word (and soon, Excel) output from a desktop application. MS Office has great support for HTML + XML + CSS as a file format, meaning that if I can create a carefully-crafted HTML document, I can generate Word output. I prefer this to creating native Word output for a number of reasons, not the least because I also need to generate HTML output.Word properly handles MHTML files, which makes for a nice single-file solution for output with embedded images.So, I'd like to dynamically generate MHTML to a stream, using API calls (not files) to construct the input.
Patrick Linskey
Thanks for the info. We've had a lot of feedback from people wanting MHTML support in System.Net.Mail and unfortunately it is not being added for .Net 4.0, however it's pretty high on the list of features for future releases of System.Net.Mail. I'll give your scenario information to our PM.
Jeff Tucker
Update: our team is officially tracking this request for future development so this will likely become supported somehow for the future in System.Net.Mail. Check the NCL blog (blogs.msdn.com/ncl) for updates on this and other new features.
Jeff Tucker
Thanks for the update! Do you guys maintain a publicly-available issue tracking system that I can register to receive updates from?
Patrick Linskey
Normally bugs are in MS Connect (connect.microsoft.com) but that's only for bugs and if you ask for a new feature there it's most likely going to get closed. This won't make it in with .NET 4.0 and .NET 5.0 is still pretty far out. There's a few other possibilities as well though that have been discussed. The NCL blog is really going to be your best place for updates. If we add new features they always get posted up there.
Jeff Tucker