How read HTML contents into a string?

V

VB Programmer

I have an HTML file that I want to read on the fly and insert the contents
into a string (for the body of an email.) How can I do this?
 
L

Lau Lei Cheong

If the HTML file is located on your server, just read it as ordinary text
file and perform string.Replace() on the placeholders
you reserved.
 
V

VB Programmer

I wrote a function which reads it ok. (See below)

The only probelm is that the HTML shows up in the email message with the raw
code, even though I set IsBodyHtml to true. Any ideas?

Here's the code to read the html...

Private Function ReadHtmlPage(ByVal url As String) As String

Dim file As String = Server.MapPath(url)

Dim sr As System.IO.StreamReader

Dim fi As New System.IO.FileInfo(file)

Dim strContents As String = ""

If System.IO.File.Exists(file) Then

sr = System.IO.File.OpenText(file)

strContents += Server.HtmlEncode(sr.ReadToEnd())

sr.Close()

End If

Return strContents

End Function
 
L

Lau Lei Cheong

If you can see the raw code, then the "read" part should be OK.

I believe you should post the "send" code in order for us to find out what's
wrong.
 
R

Russell

Try changing

strContents += Server.HtmlEncode(sr.ReadToEnd())

to

strContents += sr.ReadToEnd()

You are actually destroying your tags at that point because the HTML
encoding converts '<' to "&lt;", '>' to "&gt;", etc, which then appear
as literal characters in your email.
 
V

VB Programmer

Bingo! Thanks alot Russell (and everyone else!!)

Try changing

strContents += Server.HtmlEncode(sr.ReadToEnd())

to

strContents += sr.ReadToEnd()

You are actually destroying your tags at that point because the HTML
encoding converts '<' to "&lt;", '>' to "&gt;", etc, which then appear
as literal characters in your email.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top