将html注入到asp.net字符串中以获取电子邮件

时间:2012-05-30 17:16:34

标签: c# asp.net html string

目前我正在发送一封HTML电子邮件,我正在用asp.net字符串创建输出链接作为纯文本而不是超链接,这就是我想要的。生成电子邮件字符串的代码如下所示:

 Uri link = new Uri("http://www.somesite.com");

string body = "Plus, you may wish to bookmark " + link + " for your rate comparison needs."

string subject = Resources.Email.VerificationEmailSubject;

Email.SendEmail(email, subject, body);

   public static void SendEmail(string toAddress, string subject, string body)
    {
        try
        {

            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(new MailAddress(toAddress));
            mailMessage.Subject = subject;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = body;

            using (SmtpClient smtpClient = new SmtpClient())
            {
                smtpClient.Send(mailMessage);
            }
        }
        catch (Exception ex)
        {
            LogManager log = new LogManager();
            log.LogError(typeof(Email), ex);
        }
    }

最简单的方法是什么?

谢谢!

编辑1:

尝试 -

string emailContent = "Plus, you may wish to bookmark <a href='" + link + "'>www.comparsave.com</a> for your rate comparison needs.";

查看电子邮件源,该字符串由asp.net进行html编码,以在html中呈现为纯文本:&lt;和&gt;被“&amp; gt;”取代“&amp; lt;”等。如何阻止asp.net这样做?

编辑2:

好的,想通了!我有空的时候会发布答案。谢谢大家!

3 个答案:

答案 0 :(得分:3)

如果是HTML电子邮件,请执行以下操作:

"<a href='" + link + "'>" + link + "</a>"

答案 1 :(得分:1)

使用模板并将链接插入其中。 E.g。

const string template = "Plus, you may wish to bookmark <a href='{0}'>{0}</a> for your rate comparison needs."

string htmlEmailBody = String.Format(template, link);

答案 2 :(得分:0)

解决方案是在充当电子邮件模板的cshtml文件中使用Html.Raw。出于某种原因,我无法在上面发布的cs文件中调用Html.Raw。