Token with spaces? - Aspose.Network

When using TemplateEngine with a Data Table, is it possible to do a merge if the data table’s fields contain spaces? I have tried as both text and html body, but I can only get it to replace the token if there are no spaces (example: #InvoiceNum# works, while #Invoice Num# does not. Unfortunately I cannot control what the source data table looks like and need to support spaces.

Hi,

Thanks for considering Aspose.

Currently, it does not support column names with spaces.

As an alternate, is it possible for you to call the Replace function as strVariable.Replace(" ", "_") to replace the space with underscore?

Similarly, if you store the tokens names too in the variables and call replace on them.

Could you send some sample source. May be I can help to fix using this method.

We will fix this in the coming release. However, if your problem does not get resolved, please reply. We will do our best to give some hotfix.

Hello,

We have exposed a new interface for mail merge that supports such featurs. You can build a DataColumnMappingCollection to map the columns.

Please check it out:

MailMessage template = new MailMessage();

//add template field to subject
template.Subject = "Hello, #FirstName#";
template.From = new MailAddress("dgertz@mycompay.com", "dgertz@mycompay.com");
//add template field to receipt
template.To.Add(new MailAddress("#Receipt#", true));

//add template field to html body
//use GetSignment as the template routine, which will provide the same signment.
template.HtmlBody = "Dear #FirstName# #LastName#";

//create a new TemplateEngine with the template message.
TemplateEngine engine = new TemplateEngine(template);

//fill a DataTable as data source
DataTable dt = new DataTable();
dt.Columns.Add("Receipt", typeof(string));
dt.Columns.Add("First Name", typeof(string));//column with blank
dt.Columns.Add("Last Name", typeof(string));

DataRow dr;
dr = dt.NewRow();
dr["Receipt"] = "aaa@hotmail.com";
dr["First Name"] = "Nancy";
dr["Last Name"] = "Davolio";

dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "aaabb@hotmail.com";
dr["First Name"] = "Andrew";
dr["Last Name"] = "Fuller";

dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "bbb@163.com";
dr["First Name"] = "Janet";
dr["Last Name"] = "Leverling";
dt.Rows.Add(dr);

DataColumnMappingCollection mappings = new DataColumnMappingCollection();
mappings.Add(new DataColumnMapping("Receipt", "Receipt"));
mappings.Add(new DataColumnMapping("First Name", "FirstName"));//map the "First Name" to FirstName column
mappings.Add(new DataColumnMapping("Last Name", "LastName"));//maps

MailMessageCollection messages;

//create the messages from the template and datasource.
messages = engine.Instantiate(dt, mappings);