StringBuilder.AppendLine() does not make a new line
.net, email, string, stringbuilder, vb.net
Solution
It seems that your email body is of type `HTML` but you are rendering `simple text` -
If you really want it to be `simple text` then in your email object set `IsBodyHtml = false` it will render new lines without modifying your StringBuilder code -
MailMessage mail = new MailMessage();
mail.IsBodyHtml = false;
If you want HTML only then try adding after every line -
ErrorsStringBuilder.AppendLine("<br />");
Actually you are trying to build HTML output.
In HTML if you write -
<html>
<body>
Hi,
How are you?
</body>
</html>
It would result in -
Hi, How are you?
To make it work, you should enclose these two statements in to two different block elements -
In this case I am using paragraph -
<html>
<body>
<p>Hi,</p>
<p>How are you?</p>
</body>
</html>
now this will result in -
Hi,
How are you?
In your case you need to figure out HTML to render required output. Otherwise use `<br />` as a workaround.
Example for HTML with `<p>` tag -
Dim ErrorsStringBuilder As New StringBuilder
ErrorsStringBuilder.AppendLine(String.Format("<p>Status : {0} Message : {1}</p>", Results.Status, Results.ErrorMessage))
ErrorsStringBuilder.AppendLine(String.Format("<p>Failed : {0} Total : {1}</p>", Results.FailedCount, Results.TotalCount))
ErrorsStringBuilder.AppendLine("<p>Batch #: </p>" & Results.BatchNumber)
ErrorsStringBuilder.AppendLine()
ErrorsStringBuilder.AppendLine("<p>Individual Errors:</p>")
ErrorsStringBuilder.AppendLine()
For Each FailedRecord .......
ErrorsStringBuilder.AppendLine(String.Format("<p>Failed Record ID : {0} Message : {1}</p>", FailedRecord.ID, FailedRecord.ErrorText))
Next
Problem
I am generating an email when an error occurs. I am using `StringBuilder.AppendLine()` and `StringBuilder.AppendLine(String)` to build up the body of the email, however the body of my email appears as one very long line with no line breaks. For example: ``` Dim ErrorsStringBuilder As New StringBuilder ErrorsStringBuilder.AppendLine(String.Format("Status : {0} Message : {1}", Results.Status, Results.ErrorMessage)) ErrorsStringBuilder.AppendLine(String.Format("Failed : {0} Total : {1}", Results.FailedCount, Results.TotalCount)) ErrorsStringBuilder.AppendLine("Batch #: " & Results.BatchNumber) ErrorsStringBuilder.AppendLine() ErrorsStringBuilder.AppendLine("Individual Errors:") ErrorsStringBuilder.AppendLine() For Each FailedRecord ....... ErrorsStringBuilder.AppendLine(String.Format("Failed Record ID : {0} Message : {1}", FailedRecord.ID, FailedRecord.ErrorText)) Next ``` This is the description from MSDN for `.AppendLine()` and `.AppendLine(String)` respectively. Appends the default line terminator to the end of the current StringBuilder object. Appends a copy of the specified string followed by the default line terminator to the end of the current StringBuilder object. My question is: why is there no line break? Why is the default line terminator not being applied? Am I misunderstanding the description? ps. I did look at this question but there is no explaination to why. and I know that I can use `System.Environment.NewLine` or `"\n"` to get linebreaks.