user1486774
user1486774

Reputation: 107

How do I format stringbuilder to send my emails in tables rather then just plain text

So I have a page where the users fill out a comments / request form and when the press submit all of the information they fill out gets sent right to my email.

I am trying to make it so when the email comes through it is easier to read and in tables or html. How do I do this?

Here is my code:

System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.AppendFormat("Request Name:  {0}, <b><b/>  <br/><br/>",   txtBugName.Text.Trim());
            sb.AppendFormat("Category: {0}<br/><br/>", ddlModule.SelectedValue);
            sb.AppendFormat("Sub-Category: {0}<br/><br/>", ddlPage.SelectedValue);
            sb.AppendFormat("Description: {0}<br/><br/>", txtComments.Text.Trim());
            sb.AppendFormat("Email is: {0}<br/><br/>", txtemail.Text.Trim());

What do I need to add to change the format?

Upvotes: 1

Views: 8556

Answers (2)

Gerald Versluis
Gerald Versluis

Reputation: 34128

Your code should look more like:

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<table>");
sb.AppendFormat("<tr><td>Request Name:</td><td>{0}</td></tr>", txtBugName.Text.Trim());
sb.AppendFormat("<tr><td>Category:</td><td>{0}</td></tr>", ddlModule.SelectedValue);
sb.AppendFormat("<tr><td>Sub-Category:</td><td>{0}</td></tr>", ddlPage.SelectedValue);
sb.AppendFormat("<tr><td>Description:</td><td>{0}</td></tr>", txtComments.Text.Trim());
sb.AppendFormat("<tr><td>Email is:</td><td>{0}</td></tr>", txtemail.Text.Trim());
sb.Append("<table>");

Then I'm assuming the IsBodyHtml property is true, since you were using HTML already

Upvotes: 3

Alex
Alex

Reputation: 35407

You would set the MailMessage's IsBodyHtml property to true:

var message = new MailMessage();

message.IsBodyHtml = true;

message.Body = "your html snippet";

System.Net.Mail.MailMessage

Upvotes: 2

Related Questions