我有一个电子邮件正文模板,用于向客户发送电子邮件,其中包含发货跟踪编号和跟踪站点的链接。
逻辑使我能够使用此模板包括多达5个跟踪号和链接。如果客户只有2个跟踪号,逻辑应该删除其他三个字段。
电子邮件正文html模板:
<body>
Dear [FIRSTNAME]{LAST NAME},
Your package was shipped via USPS using tracking # [TRACKINGNUMBER1], [TRACKINGNUMBER2], [TRACKINGNUMBER3], [TRACKINGNUMBER4], [TRACKINGNUMBER5]
Track your package(s) : <a href="https://xUSPSx//[TRACKINGNUMBER1]">Click Here (#1)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER2]">Click Here (#2)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER3]">Click Here (#3)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER4]">Click Here (#4)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER5]">Click Here (#5)</a>
Thank You.
</body>
控制器逻辑:
If(orderinfo.shipmethod=="FedEx")
{
emailtemplate=emailtemplate.replace("Your package was shipped via USPS","Your package was shipped via FedEx");
emailtemplate=emailtemplate.replace("https://xUSPSx//","https://xFEDEXx//");
}
string trackingnos[]=// receives all tracking nos
int arraylength=trackingnos.length;
for(int i=0; i<arraylength;i++)
{
emailtemplate=emailtemplate.replace("[TRACKINGNUMBER" + (i+1) + "], trackingnos[i]);
}
for(;arraylength<=5; arraylength++)
{
emailtemplate=emailtemplate.replace(", [TRACKINGNUMBER" + (i+1) + "], "");
emailtemplate=emailtemplate.replace(", Click Here (#" + arraylength + ")", "" );
}
下面的控制器逻辑用于替换跟踪编号,但是空白字段的超链接仍然没有被替换。你能建议纠正我的逻辑吗?
如果要替换所有内容,为什么要使用模板呢?为什么不从头开始构建它呢?
这是我在我的程序中根据你的情况调整的方法。
private void SendEmail() //parameters if needed Client client, OrderInfo orderinfo
{
string trackingnos[] = ;// receives all tracking nos
int arraylength = trackingnos.length;
StringBuilder emailtemplate = new StringBuilder();
emailtemplate.Append("<body> Dear " + client.FIRSTNAME + " " + client.LastNAME + ", <br/>");
emailtemplate.Append("Your package was shipped via " + orderinfo.shipmethod + "using tracking # ");
for (int i = 0; i < arraylength; i++)
{
if (i != arraylength - 1)
{
emailtemplate.Append(trackingnos[i] + ", ");
}
else
{
emailtemplate.Append(trackingnos[i] + ", <br/>");
}
}
emailtemplate.Append("Track your package(s) : <br/>");
for (int i = 0; i < arraylength; i++)
{
emailtemplate.Append("<a href="https://x"+orderinfo.shipmethod+"x//" + trackingnos[i] +"">Click Here (#"+i+")</a> <br/>");
}
emailtemplate.Append("Thank you. <br/> </body> ");
Microsoft.Office.Interop.Outlook.MailItem eMail = OutlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
eMail.Subject = "Package shipped";
eMail.To = client.Email;
eMail.HTMLBody = emailtemplate.ToString();
eMail.Send();
}