Question

I am displaying an incoming e-mail in a WebBrowser control. If the email is in HTML, links are clickable and users are able to navigate to the URL quickly in their default browser. If the email is in Plain Text, however, I'm simply setting the WebBrowser's InnerText equal to the text of the email.

This leaves me with URLs that do not have anchor tags, and users have to copy and paste the URL into their browsers.

My first instinct was just to set the InnerHTML to the email text, use a regex to find any URLs and replace the matches with the same thing but with anchor tags.

This presented the problem of removing all line breaks, so I just replaced those with the appropriate tag.

public static string CheckPlainTextLinks(string html)
{
  Regex regx = new Regex(@"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", RegexOptions.IgnoreCase);
  MatchCollection mactches = regx.Matches(html);

  foreach (Match match in mactches)
  {
    html = html.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
  }

  html = html.Replace(Environment.NewLine, "<br />");

  return html;
}

This is the entirety of my function to scan the text and add links. I then set the InnerHTML of my webBrowser control to what is returned by this function. Unfortunately, the program started getting OutOfMemory Exceptions that went away when my call to this function was taken out.

I looked into using mshtml to create the links instead of changing the html directly with help from these posts, http://social.msdn.microsoft.com/Forums/da-DK/csharpgeneral/thread/1d050260-3625-42cc-94ec-59bba0651a1c. I'm just not sure how to create the IHTMLTxtRange on each of the regex matches.

Is there a better way to create these links or a solution to the out of memory exception?

Was it helpful?

Solution

Would this work better for you?

public static string CheckPlainTextLinks(string html) {
    var regx=new Regex(@"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", RegexOptions.IgnoreCase);
    return regx.Replace(html, x => "<a href='"+x.Value+"'>"+x.Value+"</a>").Replace(Environment.NewLine, "<br />");
}

Instead of repeatly replace in a foreach loop, this works with MatchEvaluator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top