Why does StringBuilder.AppendLine not add a new li

2019-02-21 09:35发布

问题:

I'm trying to use stringbuilder to create a body of string to be used in a text (not HTML) email. However some lines (where i include dynamic data, a new line is not added, but in some the newline works as intended.

Is there something basic i'm missing when using the stringbuilder class or is there some more fundamental process that should be happening?

in the below code:

sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email someone@somewhere.com");
sbUser.AppendLine();
sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy"));
sbUser.AppendLine("==============================================================");
sbUser.AppendLine();

(ContentPage and thisEvent are custom classes built using Subsonic(v2). PageTitle is an output type of string)

is get this as an output:

    Please find below confirmation of your registration details. If any of these details are incorrect, please email someone@somewhere.com

Selected event : My Event Date of event : 16 Sept 2012 ==============================================================

as you can see, everything after the 3rd line in the code makes everything go on to one line.

however, further down the code i use:

sbRR.AppendLine("First name : " + txtFirstname.Text.Trim());
sbRR.AppendLine("Surname : " + txtSurname.Text.Trim());
etc,

and all these appear on seperate lines correctly. I can't see why this is happening.

the email is composed as such

mailMessage.Body = sbUser.ToString() + sbRR.ToString();


adding the following code:

sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle + Environment.NewLine); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy") + Environment.NewLine);

produces the following output:

Selected event : My Event

Date of event : 16 Sept 2012

==============================================================

which works i suppose, except it's added 2 newlines (the AppendLine and the Environment.NewLine). it seems that pulling the data directly straight from the database into a stringbuilder seems to be messing with the line ending. Even if I add text after the database pull, it still stays on one line.

UPDATE

doing StringBuilder.Append("blah"+Environment.NewLine) produces the correct result, however i'm still not understanding why that works and .AppendLine("blah"+<database content>) doesn't work.

回答1:

Instead of

sbUser.AppendLine();

Try using

sbUser.Append(Environment.NewLine);

No idea why this works...



回答2:

I know the question is old and has been marked as answered, but I thought I'd add this here in case anyone else comes across this as it's the first hit on Google for StringBuilder.AppendLine() not working.

I had the same problem and it turned out to be an Outlook issue. Outlook re-formats text based emails by removing extra line breaks. You can click "We removed extra line breaks in this message -> Restore line breaks" in the header of the individual email, or change the setting that does this nasty little trick "Options->Mail->Message Format->Remove extra line breaks in plain text messages"

The workaround (since you can't control the settings on every potential email target) I found here Newsletter Formatting And The Remove Extra Line Breaks Issue. Basically, if you add two white space characters to the beginning of each line, Outlook won't reformat the email.

Here's an extension method to help (method name is a bit verbose so change to your liking :))

namespace System.Text
{
    public static class StringBuilderExtensions
    {
        public static void AppendLineWithTwoWhiteSpacePrefix(this StringBuilder sb, string value)
        {
            sb.AppendFormat("{0}{1}{2}", "  ", value, Environment.NewLine);
        }

        public static void AppendLineWithTwoWhiteSpacePrefix(this StringBuilder sb)
        {
            sb.AppendFormat("{0}{1}", "  ", Environment.NewLine);
        }
    }
}


回答3:

use Environment.NewLine

sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email someone@somewhere.com");
sbUser.AppendLine(Environment.NewLine);
sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 
sbUser.AppendLine("Date of event : " + thisEvent.EventStartDate.ToString("dd MMM yyyy"));
sbUser.AppendLine("==============================================================");
sbUser.AppendLine(Environment.NewLine);


回答4:

use Environment.NewLine after each line or where you want new line

eg:-

  sbUser.AppendLine("Please find below confirmation of your registration details. If any of these details are incorrect, please email someone@somewhere.com" + Environment.NewLine);
  sbUser.AppendLine("Selected event : " + ContentPage.FetchByID(int.Parse(ddlEvent.SelectedValue)).PageTitle); 


回答5:

Windows 10 Insider preview Build 15007. The Default Line Terminator and the Environment.NewLine are both "\n". To use "\r\n" I had to create a string constant and use it instead.



回答6:

First sbUser.Appendline();

Second sbUser.Append("texto loco ");

Voila!

=)