2

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.

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

-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.
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.
How can I provide URL, author name in this, please help
2

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.

4 Comments

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
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.
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.
@Zero Use placeholders that will fit the syntax for string.Format
1
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?

1 Comment

Hi Fyodor, This is working fine for me, but I want to assign values in between these code, like between these <td> myVariable </td>
0

Use @" then your html( remember replace " with "" ) then close " and ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.