views:

168

answers:

3

I am using asp.net and C#. I want to send mail to my user in HTML format, I have the content in HTML format let say like this

    <table style="width:100%;">
     <tr>
       <td style="width:20%; background-color:Blue;"></td>
       <td style="width:80%; background-color:Green;"></td>
     </tr>
    </table>

Now I am unable to assign this to a string variable, so that I could send it as a mail. Please let me know how can I bind this whole HTML content into a varibale.

Also, please note that the above code is only a demo, I have around 100 lines of HTML code.

A: 

If you want to explicitly declare the string in code:

string html =
@"<table style=""width:100%;""> 
     <tr> 
       <td style=""width:20%; background-color:Blue;""></td> 
       <td style=""width:80%; background-color:Green;""></td> 
     </tr> 
    </table>";

In response to your comment, to insert values, it's simple enough to use StringBuilder to build a string in memory, eg.,

var html = new StringBuilder("<table style=\"width:100%;\">");
html.Append("<tr>");
html.Append("<td style=\"width:20%; background-color:Blue;\">");
html.Append(yourAuthorNameString);
//etc...

or move to a proper html builder or template system like the HTML Agility Pack or NVelocity

jball
-1: Escape sequence `\"` will not work as you expect it to in a string that is prepended with `@`. Use a doubled double quote (he-he) instead.
Fyodor Soikin
Was just fixing that. Prefixed the @ after I'd escaped the string in case the questioner wanted to preserve newlines, and forgot to switch the escaping.
jball
Ok, cancelled the -1.
Fyodor Soikin
How can I provide URL, author name in this, please help
Zerotoinfinite
Thanks jball...you just saved my day.... :)
Zerotoinfinite
+1  A: 
string myHtml = @"<table style=""width:100%;"">
     <tr>
       <td style=""width:20%; background-color:Blue;""></td>
       <td style=""width:80%; background-color:Green;""></td>
     </tr>
    </table>";

Or did I misunderstand your question? In that case, what problem do you encounter and at what stage?

Fyodor Soikin
Hi Fyodor,This is working fine for me, but I want to assign values in between these code, like between these <td> myVariable </td>
Zerotoinfinite
+1  A: 

I would just keep it in an html file that you open and read in as needed. Good old System.IO.File.ReadAllText(). Putting a large string directly in your source is just begging for frequent re-compilation and deployment.

Joel Coehoorn
This is great, but my problem is this that I have to put my parameter into that HTML code, like the Member Name, Article name, URL etc... How can I achieve this ? Please help
Zerotoinfinite
You got an answer to the question that you've asked. If you wanted an answer to a different question, you should've asked that question in the first place. I suggest you go ahead and open another question.
Fyodor Soikin
Hi Fyodor,I appreciate your concern, but do you want me to post another question to just ask how can I put a variable inside that HTML example provided by you, even in that case I have to provide the link to this post to make it easier to user to understand the issue.
Zerotoinfinite
@Zero Use placeholders that will fit the syntax for string.Format
Joel Coehoorn