tags:

views:

494

answers:

3

I am writing a SharePoint timer job, which needs to pull the content of a web page, and send that HTML as an email.

I am using HttpWebRequest and HttpWebResponse objects to pull the content.

The emailing functionality works fine except for one problem.

The web page which serves up the content of my email contains images.

When the html of the page is sent as an email, the image URLs inside the HTML code are all relative URLs, they are not resolved as an absolute URL.

How do i resolve the image URLs to their absolute paths inside the web page content?

Is there any straight forward way to do this? I don't want to run a Regex over the html code to replace all relative URLs with absolute URLS.

+5  A: 

Try adding a base element to the head of the html document you retrieve. As href attribute you should use the url of the page you are retrieving.

Ronald Wildenberg
A: 

I don't want to run a Regex over the html code to replace all relative URLs with absolute URLS.

Too bad, because that's the only way you'll get the images to show up. Would you rather download all the images and embed them in the email too?

Al W
A: 

Found this cool Codeplex tool called HtmlAgilityPack. http://www.codeplex.com/htmlagilitypack

Using this API, we can parse Html like we can parse XML documents. We can also query and search nodes using XPath.

I used the following code snippet to fix the Image URLs

HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmlMessage);

//This selects all the Image Nodes
            HtmlNodeCollection hrefNodes = htmlDoc.DocumentNode.SelectNodes("//img");

            foreach (HtmlNode node in hrefNodes)
            {
                string imgUrl = node.Attributes["src"].Value;
                node.Attributes["src"].Value = webAppUrl + imgUrl;
            }

     StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);

            htmlDoc.OptionOutputAsXml = false;
            htmlDoc.Save(sw);
            htmlMessage = sb.ToString();
ashwnacharya