0

Currently I'm sending an HTML e-mail I'm creating out of asp.net strings is outputting links as plain text rather than hyperlinks, which is what I'm going for. The code for generating the e-mail string looks like this:

 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);
        }
    }

What is the simplest way to do this?

Thanks!

Edit 1:

Tried -

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

Viewing the email source, the string is being html encoded by asp.net to render as plain text in html: < and > are being replaced by "& gt;" "& lt;" etc. How to stop asp.net from doing this??

Edit 2:

Okay, figured it out! Will post the answer later when I have time. Thanks all!

1
  • can you also post the code calling that method? Commented May 30, 2012 at 18:32

3 Answers 3

3

If it's an HTML email, do this:

"<a href='" + link + "'>" + link + "</a>"
Sign up to request clarification or add additional context in comments.

9 Comments

This gives me: Plus, you may wish to bookmark <a href='comparasave.com/'>www.comparsave.com</a> for your rate comparison needs.
That <a> tag should disappear when the email arrives in your inbox, and you should just see link, as long as you are sending it as HTML.
I'm viewing it as a .mht file in internet explorer. Any idea if this would be giving it problems?
I don't know what .mht is, but yes, probably. Can you try looking at it in your email (Outlook, Gmail, etc.)?
Viewed it in outlook...same problem :(
|
1

Use a template and insert your links into it. 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);

Comments

0

The solution was to use Html.Raw from within a cshtml file acting as the email template. For some reason I wasn't able to call Html.Raw in the cs file posted above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.