We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Generate Emails w/Mail Merge


I am looking for sample code to generate email messages with from, to, subject etc using the mail merge capability in Aspose.

The To and Subject should be autogenerated based on mail merge source.

The end result needs to be an email message which I can send using Aspose.Net or my custom email sending .NEt code.

This is what i have so far with comments on what i would like it to be

string dir = @".\..\..\";

DataSet customersDs = new DataSet();

customersDs.ReadXml(dir + "MailMergeData.xml");

Document doc = new Document(dir + @"MailMergeDoc.docx");

doc.MailMergeSettings.Destination = MailMergeDestination.Email;

doc.MailMergeSettings.MailSubject = "<>"; // Would like this merged from the template file

doc.MailMergeSettings.AddressFieldName = "a.b@gmail.com"; //Email Address changes. Read from template file


doc.MailMergeSettings.Destination = Aspose.Words.Settings.MailMergeDestination.Email;

doc.Save(dir + "TestFile Out.doc"); // Need the output to be email messages. Not a doc file.

Can you provide me or point me to sample code which meets the above functionality?



Thank you for inquiry.

Please refer to http://www.aspose.com/documentation/.net-components/aspose.network-for-.net/performing-mail-merge.html page for necessary documentation and code sample to perform mail merge. The subject, from, to etc can be replaced with the values in the dataset.

Thanks for the prompt response.

I had seen the link provided. In this link, the body of the email is set in code.

Can you provide a variation of the samle where

- Body of email is generated using Mail merge from MS Word template

- Email address is correlated in the To so that the correct email address corresponds to the name in tbe body



Thanks for your request. I think, in your case, you can use a combination of the approaches suggested in the following articles:



The first article describes how you can use mail merge to generate a body for your e-mails, and the second shows how you can send the e-mails.

Hope this helps.

Best regards,

I think we are missing a basic point here.

The output of the first link is an MS Word document which has the merged version of the word template and the data.

The output of the second link is a way to set to the To, Body etc for the sender.

How do I correlate the specific messages in the word document with the to and body?



Thanks for your request. If I understand your requirements correctly, all data (data that should be merged into the body and recipient’s e-mail address) are stored in database. Each row in your data source represents data that should be merged into template and sent to a specific recipient. am I right?

That is why I suggested you to take a look at the article that demonstrated how to generate a separate document for each record in your data source. What you should do is simply get a recipient address from each record in your data source and send a document generated for this record as described in the second article.

Best regards,