您需要为 MailMessage 的正文启用 HTML,如下所示:
o.IsBodyHtml = true;
也许您应该选择另一个构造函数,以使代码更具可读性。可能是这样的:
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender@domain.com", "Customer Service");
mailMessage.To.Add(new MailAddress("someone@domain.com"));
mailMessage.Subject = "A descriptive subject";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "Body containing <strong>HTML</strong>";
完整文档:http://msdn.microsoft.com/en-us/library/System.Net.Mail.MailMessage(v=vs.110).aspx
更新
似乎是您的字符串构建给您带来了麻烦。有时,将字符串放在一起(或将它们连接起来)时,要使所有引号都正确是很棘手的。在创建像电子邮件这样的大字符串时,有一些选项可以让它正确。
首先,常规字符串 - 缺点是难以阅读
string body = "Hello, " + name + "\n Your KAUH Account about to activate click the link below to complete the actination process \n <a href=\"http://localhost:49496/Activated.aspx">login</a>";
第二,逐字字符串 - 允许代码中的换行符提高可读性。请注意开头的 @ 字符,并且引号转义序列从 \" 更改为 ""。
string body = @"Hello, " + name + "\n Your KAUH Account about to
activate click the link below to complete the actination process \n
<a href=""http://localhost:49496/Activated.aspx"">login</a>"
第三,字符串生成器。这实际上是许多方面的首选方式。
var body = new StringBuilder();
body.AppendFormat("Hello, {0}\n", name);
body.AppendLine(@"Your KAUH Account about to activate click
the link below to complete the actination process");
body.AppendLine("<a href=\"http://localhost:49496/Activated.aspx\">login</a>");
mailMessage.Body = body.ToString();
StringBuilder 文档:http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx