Reputation: 845
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!
Upvotes: 0
Views: 1898
Reputation: 845
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.
Upvotes: 0
Reputation: 43107
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);
Upvotes: 1
Reputation: 11004
If it's an HTML email, do this:
"<a href='" + link + "'>" + link + "</a>"
Upvotes: 3