0

What I want to do is;

for (int i = 0; i < thexfiles.Length; i++)
{
    tosend = tosend + "<tr><td>"+thexfiles[i]+"</td><td>"+thexdates[i]+"</td></tr><tr>";
}
mail.Body = tosend;

I want to insert the data into a table in a C# code (using html perhaps?), so when it's mailed it looks clean.

5
  • So what exactly is the problem? You're already showing us your own answer... Commented Jul 12, 2012 at 7:23
  • well that code doesnt work, it just sends <tr><td>+thexfiles[i]+</td><td>+thexdates[i]+</td></tr><tr> to mail Commented Jul 12, 2012 at 7:25
  • Add the table tags and set mail.IsBodyHtml = true; Commented Jul 12, 2012 at 7:26
  • 2
    Please use google search or stackoverflow search before asking - there are plenty info there stackoverflow.com/questions/1329922/… Commented Jul 12, 2012 at 7:27
  • What type is thexfiles? Have you tried thexfiles[i].ToString()? Commented Jul 12, 2012 at 7:27

3 Answers 3

3

Try this

    StringBuilder sb = new StringBuilder();
    sb.Append("<table>");
    sb.Append("<tr><td>Column 1</td><td>Column 2</td></tr>");

    for (int i = 0; i < thexfiles.Length; i++)
        sb.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", thexfiles[i], thexdates[i]);

    sb.Append("</table>");

    mail.IsBodyHtml = true;
    mail.Body = sb.ToString();
Sign up to request clarification or add additional context in comments.

4 Comments

you forgot the <tbody> tag :)
<tbody> is an optional tag, and now a days we would use <div> to create the layout
how would i add another line just outside the loop, to show the column names ??
You should use <th> rather than <td> for the column names.
2

if you are using the class: System.Net.Mail.MailMessage make sure after assigning the content to the Body property you also set the property IsBodyHtml to true .

that should work.

Comments

1

You simply have to do this:

for (int i = 0; i < thexfiles.Length; i++)
{
    tosend = tosend + "<tr><td>"+thexfiles[i]+"</td><td>"+thexdates[i]+"</td></tr>";
}

tosend = "<html><table>" + tosend + "</table></html>";

mail.IsBodyHtml = true;
mail.Body = tosend;

And that's it, the mail body will be shown in HTML table.

1 Comment

+ is just for string concatenation..and how can you say that it's slower..it is faster than StringBuilder.Append

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.