我正在尝试发送包含链接的电子邮件,但它无法正常运行。
这就是我所拥有的:
mail.Body = mail.Body + "<div>" + "<p><a href=" +
"http://serverName/projectName/controllerName/AllSummaries?yesterday=" +
DateTime.Today.AddDays(-1).Date.ToString("MM/dd/yyyy") + ">Click here to go to " +
DateTime.Today.AddDays(-1).Date.ToString("MM/dd/yyyy") + " summaries!</a></p>";
mail.IsBodyHtml = true;
电子邮件中的结果是:
http://serverName/projectName/controllerName/AllSummaries?yesterday=04/04/2017
不应该是:
Click here to go to 04/04/2017 summaries!
?
我错过了什么?
更新
字符串插值:
var date = DateTime.Today.AddDays(-1).Date.ToString("MM/dd/yyyy");
mail.Body += $"<div><p><a href='http://serverName/projectName/controllerName/AllSummaries?yesterday={date}'>Click here to go to {date} summaries</a></p></div>";
结果:
我仍然看到完整的链接..而不是Click here to go to {date} summaries
使用String.Format
答案 0 :(得分:3)
确保您将href
属性包含在引号内,并为每个现有属性设置正确的结束标记。
您可能需要考虑使用String.Format()
方法来处理此问题(或者像String Interpolation这样的新方法)
// Using String.Format()
mail.Body += String.Format("<div><p><a href='http://serverName/projectName/controllerName/AllSummaries?yesterday={0}'>Click here to go to {0} summaries</a></p></div>", DateTime.Today.AddDays(-1).Date.ToString("MM/dd/yyyy"));
// Using String.Interpolation
var date = DateTime.Today.AddDays(-1).Date.ToString("MM/dd/yyyy");
mail.Body += $"<div><p><a href='http://serverName/projectName/controllerName/AllSummaries?yesterday={date}'>Click here to go to {date} summaries</a></p></div>";