将有效的html链接插入电子邮件的字符串中

时间:2012-12-19 19:38:49

标签: c#

我正在构建一个将通过电子邮件发送的字符串。在字符串中,我想包含一个链接,如下所示:

String mailstring = "Blah blah blah blah. Click here for more information."

我希望“here”成为电子邮件中的链接,例如将其设为http://madeuplink.com。我知道我可以把地址而不是'here',但我希望链接是单词。

3 个答案:

答案 0 :(得分:5)

您可以添加HTML标记。假设客户端启用了HTML电子邮件,它应该成为一个链接。如果您使用MailDefinition创建电子邮件,请确保IsBodyHtml属性设置为true。

String mailstring = "Blah blah blah blah. Click <a href=\"http://www.example.com\">here</a> for more information."

答案 1 :(得分:1)

 string input = String.Format("Blah blah blah blah. Click {0} for more information.", 
                    "<a href=\"http://www.example.com\">here</a>");

OR

 string input = "Blah blah blah blah. Click <a href=\"http://www.example.com\">here</a> for more information.", 

答案 2 :(得分:1)

添加到@ keyboardP的注释只需要注意一件事,即使它有点脱离上下文,如果使用SmtpClient将MailMessage对象作为消息发送,则需要设置MailMessage.IsBodyHtml = true

public void SendEmail(string to, string subject, string body) { MailMessage mail = new MailMessage("someone@example.com", to); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = true; ... }

只有设置了此属性后,HTML标记才会在电子邮件中显示所需的表单,否则它会将标记显示为文本而不是超链接。