Need to do a mail merge for ASP.NET application.
Considered just programatically using word directly but this has liscening implications and is not recommended (ht开发者_StackOverflowtp://support.microsoft.com/kb/257757)
So was looking into openXML / Word Automation Services. But not sure if can easily achieve a mail merge via this?
We had such requirement 4-5 years back and then we decided to go with custom self-made solution. Here's the outline of solution that may help you:
- Use rich text box to capture mail template (users can create their own mails). Use special syntax to insert placeholders for your data within the template. So what you have is a html body containing placeholders.
- When mails needs to be generated for N records, for each record, parse the template html to search for placeholders (can be done using reg-ex) and replace them with relevant data.
- Generated html can be emailed (or it can be converted into pdf or word using libraries)
In reality, this may mean a lot of work based on actual requirements. For example, in our case
- We have a metadata repository that describes business entities and their attributes
- GUI for end users to define the template. UI allows multi-part templates (e.g. if you need to send covering letter with attachments), insertion of various placeholders (we support fields, entity groups, simple filtering etc), format for a part (html/pdf/doc), details about email/fax recipients (in terms of metadata)
- Entity Grouping Support - means essentially that ability have child data. For example, for printing letter about an order, you have to put a table for items (products) ordered within an order. There can be any level of such nesting.
- To be efficient, there is runtime that parse template contents only once and then only do substitution. We also have query engine that converts metadata placeholders into a query to fetch data etc
It would be great if there is any pre-built tool for such thing but as such I am not aware of anything.
EDIT: All above is relevant if your requirements is to have user defined mail templates. If you have only predefined set of templates then solution can be a lot simpler - for example, (as Yves pointed out) using aspx files with data binding expressions to get the html/xml body and then convert it into relevant format. You can then use HttpServerUtility.Execute to capture page output for any number of records.
精彩评论