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

Free Support Forum - aspose.com

Dynamic fields in .msg file

Hi

I've created a email template in Outlook and saved it as a .Msg file. I want to put dynamic fields in the email body. How can I use Aspose.Email to populate these dynamic fields.

See attached example, I have #Name# where i get the persons name from a database. How can I replace these fields in the .Msg file?

thanks

AJ

Hi Arjuna,


Thank you for using Aspose.Email.

Please refer to the following code that performs the task you are in need of. Please let us know if you have any additional query/inquiry related to Aspose.Email.

C# Sample Code:

MailMessage mailMsg = MailMessage.Load(“AsposeTemplate.msg”);<o:p></o:p>

//Create a new TemplateEngine with the msg message.

TemplateEngine engine = new TemplateEngine(mailMsg);

//Create an instance of DataTable

//Fill a DataTable as data source

DataTable dt = new DataTable();

dt.Columns.Add("Name", typeof(string));

//Create an instance of DataRow

DataRow dr;

dr = dt.NewRow();

dr["Name"] = "Kashif";

dt.Rows.Add(dr);

MailMessageCollection messages;

try

{

//Create messages from the message and datasource.

messages = engine.Instantiate(dt);

MailMessage msg = messages[0];

//Save the message

msg.Save("Test.msg", MailMessageSaveType.OutlookMessageFormat);

//or send it using Smtp

//Create an instance of SmtpClient and specify server, port, username and password

SmtpClient client = new SmtpClient("smtp.server.com", 25, "username", "password");

//Send messages in bulk

client.BulkSend(messages);

}

catch (MailException ex)

{

System.Diagnostics.Debug.WriteLine(ex.ToString());

}

catch (SmtpException ex)

{

System.Diagnostics.Debug.WriteLine(ex.ToString());

}