如何在MailMessage.Body Property </sometext>中以<sometext>格式传递字符串

时间:2015-04-21 09:33:52

标签: c#

我需要在角括号中传递一些文字,例如&lt;头&GT;或者&lt;脚本&GT;在MsgBody部分,我无法这样做......我的代码 -

     MailMessage msg = new MailMessage();
     msg.Body = string.Format("<b>System Area :</b>{0}<br /><b>Assigned To : </b>{1}<br /><b>Status :</b><font color=#0000FF>{2}</font><br /><br><b>Description :</b> {3}", system_Area, Assigned_ToVal, TaskStatus, MsgBody);             

1 个答案:

答案 0 :(得分:0)

最好的选择是HtmlEncode所需的文本正文并使用&#34; @&#34;来转义整个字符串:

MailMessage msg = new MailMessage();
string system_Area_Encoded = HttpContext.Current.Server.HtmlEncode(system_Area);
string assigned_ToVal_Encoded = HttpContext.Current.Server.HtmlEncode(Assigned_ToVal);
string taskStatus_Encoded = HttpContext.Current.Server.HtmlEncode(TaskStatus);
string msgBody_Encoded = HttpContext.Current.Server.HtmlEncode(MsgBody);
msg.Body = string.Format(@"<b>System Area :</b>{0}<br /><b>Assigned To : </b>{1}<br /><b>Status :</b><font color=#0000FF>{2}</font><br /><br><b>Description :</b> {3}", system_Area_Encoded, assigned_ToVal_Encoded, taskStatus_Encoded, msgBody_Encoded); 

您可以在MSDN中获得有关HtmlEncode的更多信息: https://msdn.microsoft.com/es-es/library/w3te6wfz(v=vs.110).aspx