views:

59

answers:

2

Hi, I'm just wondering about this one. I'm creating an ASP.NET webform containing lots of textboxes etc. And I want to send an e-mail based on this stuff. And the e-mails body needs to go into a string.

And the string is supposed to contain HTML code, but the syntax changes because of the string. So how can I simplify this? Is there any software or something that lets me do this? Perhaps paste in some HTML code and then convert this to string format, ready to use with vb.net?

+2  A: 

If I understand correctly, you are wanting to generate an HTML email from user input via textboxes. If so, you can easily generate HTML using the HtmlTextWriter:

StringBuilder sb = new StringBuilder();
using(HtmlTextWriter writer = new HtmlTextWriter(new StringWriter(sb)))
{
    writer.WriteBeginTag("span");
    writer.WriteAttribute("style", "font-weight:bold;");
    writer.Write(HtmlTextWriter.TagRightChar);
    writer.Write(textboxName.Text);
    writer.WriteEndTag("span");
}
return sb.ToString();

The above will generate a string which looks like:

<span style="font-weight:bold;">user input</span>

The HtmlTextWriter makes it very easy to ensure your markup is semantically correct and valid by mimicking the XML writers.

Rex M
Is this really the most efficient way? Wont this need a huge code, just for some plain html? What if I wanted to create a much bigger HTML string? Would it be smart to add the HTML code to a textfile and then just read the textfile as a string?
Kenny Bones
A: 

For converting a HTML formatted string back to plain text try this link,

[http://www.dreamincode.net/code/snippet1578.htm%5D%5BStrip HTML from string using Regular Expressions]

This will only strip the tags and doesn't remove the html symbols like an ampersand but it will get you started.

madC