如何在 asp.net 代码中正确组合html?



我正在尝试使用 ASP.NET C# 发送电子邮件。电子邮件发送得很好。 问题是我试图在电子邮件中嵌入链接,但客户端的链接无法识别(因为浏览器无法将其识别为链接(

我尝试将 C# 放入一个变量中,我尝试将代码拆分为各种变量,但我不太确定我在这里做错了什么。这可能是我放置 ' 或 " 嵌入的方式。不太确定。


string body = "Hello " + firstNameUser + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + HttpContext.Current.Request.Url.Authority + "/Auth/Activation/Activation.aspx?ActivationCode=" + TwoFACode + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
body += "<br /><br />Support Team";
mail.Body = body;
mail.IsBodyHtml = true;
client.Send(mail);

"请点击以下链接激活您的帐户"无法识别,这意味着它不是可点击的HTML链接。在电子邮件中,您只能看到文本而不是超链接。

在我看来,您在链接中缺少某些内容:

body += "<br /><a href = 'https://" + HttpContext.Current.Request.Url.Authority + 
"/Auth/Activation/Activation.aspx?ActivationCode=" + 
TwoFACode + "'>Click here to activate your account.</a>";

添加所需的 https://或 http://应该可以修复它。

Uri.Authority 属性

The Authority property is typically a server DNS host name or IP address. 
This property might include the service port number if it differs from the 
default port for the URI. If the Authority component contains reserved 
characters, these are escaped in the string value returned by this property.

希望这有帮助。

最新更新