TemplateRoutine not working properly

Hello,


I am sending two different types of emails to the same recipient, but the signature code does not get always replaced when instantiating TemplateEngine one after the other. Only one of the two emails gets the signature placeholder properly replaced

- The htmlbody column is read from the database with data ending like this:

[rest of email wiht place holders goes here…]
#GetSignature()#
- The design pattern I utilize calls the following method to set the engine and signature:
private void SetEngineFromTemplatedMsg()
{
mEngine = new TemplateEngine(mTemplatedMsg);
mEngine.RegisterRoutine(“GetSignature”, new TemplateRoutine(GetSignature));
}
- The GetSignature method follows your recommendation
static object GetSignature(object[] args)
{
return “Bla bla Team
Bla bla Department
” + DateTime.Now.ToShortDateString();
}
- Utilizing the agent to Dispatch the Message
private void DispatchMessage()
{
using (SmtpClientBulkSendAgent agent = new SmtpClientBulkSendAgent(AsposeSingleton.instance.Client))
{
agent.AddMessages(mMessages);

agent.Start();

agent.WaitForIdle();
}
- Utilizing aspose.email for .NET 5.9, .net framework 4.0 and different windows environments
One execution runs this part of the code twice for two different templated messages. The emails gets send and the place holders get properly replaced with the exception that the #GetSignature()# place holder gets replaced in only one of the two emails

Any Ideas?

Thanks

Luis


Hi Luis,


Thank you for writing to Aspose Support team.

We are currently investigating this issue at our end and need a little more time to identify the problem at our end. We shall share our findings with you here once we are done with our investigations. We appreciate your little patience until then.

Hi Luis,

I have tried the scenario by using two place holders in the Html body part of the template message. However, I am afraid that no issue is observed and different html body with signature on each mail is sent successfully. Could you please test this scenario using latest version of Aspose.Email for .NET 6.0.0 and let us know the feedback.

//Create a new MailMessage instance
MailMessage msg = new MailMessage();

//Add subject and from address
msg.Subject = "Hello, #FirstName#";
msg.From = "from@gmail.com";

//Add email address to send email
msg.To.Add("to@gmail.com");

//Add mesage field to HTML body
msg.HtmlBody = "Your message here";
msg.HtmlBody += "Thank you for your interest in Aspose.Email.";

//Use GetSignment as the template routine, which will provide the same signature
msg.HtmlBody += "#HtmlBodyPart#
Have fun with it.

#GetSignature()#";

//Create a new TemplateEngine with the MSG message.
TemplateEngine engine = new TemplateEngine(msg);

// Register GetSignature routine. It will be used in MSG.
engine.RegisterRoutine("GetSignature", new TemplateRoutine(GetSignature));

//Create an instance of DataTable
//Fill a DataTable as data source

DataTable dt = new DataTable();
dt.Columns.Add("Receipt", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("HtmlBodyPart", typeof(string));

//Create an instance of DataRow
DataRow dr;
dr = dt.NewRow();
dr["Receipt"] = "to@gmail.com";
dr["FirstName"] = "Tom";
dr["HtmlBodyPart"] = "Group";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "to@gmail.com";
dr["FirstName"] = "Jerry";
dr["HtmlBodyPart"] = "Team";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Receipt"] = "to@gmail.com";
dr["FirstName"] = "Kyle";
dr["HtmlBodyPart"] = "Huang";
dt.Rows.Add(dr);

MailMessageCollection messages;

try
{
    //Create messages from the message and datasource.
    messages = engine.Instantiate(dt);
    
    //Create an instance of SmtpClient and specify server, port, username and password
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587, "user", "password", SecurityOptions.SSLExplicit);
    
    SmtpClientBulkSendAgent agent = new SmtpClientBulkSendAgent(client);
    agent.AddMessages(messages);
     agent.Start();
}
catch (MailException ex)
{
    System.Diagnostics.Debug.WriteLine(ex.ToString());
}
catch (SmtpException ex)
{
    System.Diagnostics.Debug.WriteLine(ex.ToString());
}

you need to put your code in a for loop and depending on I change something in the message, etc

Hi Luis,


Thank you for the feedback. You are right that if all the requirements are not fulfilled by placing place holders in message body, subject and recipients etc, you may please put your message formatting code in loop and modify it accordingly. In this way you may modify message entirely if required.

Please feel free to write us back if you have any other query related to Aspose.Email.

What I am saying is that the GetSignature functionality does not work when you put yoru code in a loop. Any ideas why?



Cordially,
Luis

Hi Luis,


When we call the TemplateEngine.Instantiate(DataTable ), it automatically iterates all the records in the table and generates set of messages. In this case the all the place holders (tags) are replaced by the data in table. However if you put the code in loop and don’t use Instantiate(), then you have to manually append the signature.

If this description does not fulfill your requirement, please send us a complete compilable project (or code) using the loop, but not appending the Signature. We will review the code and provide assistance accordingly.

I will do so whenever I find the time.

In the mean time I am not utilizing your functionality, sometimes the place holder gets replaced some time it does not. This also has happened when utilizing placeholders in the subject line, sometimes they get replaced sometimes they don't. To temporarily fix this:

- I removed the your Signature functionality and added signature info as part of the same htlmBody
- I removed any place holders form the Subject line leaving the Subject line information quite vague

The only thing that occurs to me this is happening if that I utilize a singleton pattern for the client, so the same client is always utilized, but you said in previous email that this was okay. I just don't know what causes this to work only partially.



Hi Luis,

From your posted statement and the details shared by us, we were not able to reproduce the issue with our shared sample code. It is difficult to get an idea about what code you are using to reproduce the issue and I request you again to share a sample console application that we can use to reproduce the issue at our end for assisting you further. It will help us save your time for investigating the issue and assisting you further.