1

I am writing plain text to email body using StringBuilder. the line does not gets added in all the desired places.

So far I have tried. Both don't create the desired line all the time.

builder.Append("\r\n");
builder.Append(Environment.NewLine);

here is part of my code:

builder.Append("question 1: ");
builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.GetAnswer1));
builder.Append(Environment.NewLine);
builder.Append("question 2: ");
builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.GetAnswer2));
builder.Append(Environment.NewLine);
builder.Append("question 3: ");
builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.GetAnswer3));
builder.Append(Environment.NewLine);

Result:

question 1: No
question 2: Yes question 3: No

Desired Result:

question 1: No
question 2: Yes 
question 3: No

If I add two builder.Append(Environment.NewLine) as supposed to one, it would create two double lines (but I can't get it to break a line at the third question). I have several of these questions and this happens randomly (no pattern).

Any suggestion on how to force a single line.

4
  • Please show the desired result, your description seems a bit unclear to me.
    – IS4
    Nov 3, 2014 at 17:54
  • 1
    I just updated the post with the desired result. Nov 3, 2014 at 19:09
  • 2
    Is your email being sent as HTML ? If it is you'll need to be adding <br> tags.
    – SteveB
    Nov 3, 2014 at 20:00
  • Why the random usage of caps in your method names? GetYESNOfromBOOL ... Horrible. GetYesNoFromBool would suffice. Nov 5, 2014 at 14:41

3 Answers 3

3

You've made a bad assumption; StringBuilder.Append does not append a new line. You want .AppendLine(). What you have is exactly what is expected each .Append adds the text, but no line terminator. You've added 3, so you get 3 lines.

Update based on OPs comments

Based upon your code and methods, this should be fine:

builder.AppendLine(String.Format("Full Houlse?: {0}", CommonUtilities.GetYESNOfromBOOL(bo.FullHoulse)));

Format the string you need, and append the complete line to the StringBuilder. You'll need to do this for each line you want in there.

2
  • i have already tried .ApendLine() and got same result as showed above. Nov 3, 2014 at 19:46
  • 1
    We'll need to see more of your code. I use StringBuilders all the time and have never "lost" newlines.
    – AlG
    Nov 3, 2014 at 19:55
2

Your code looks like it should work. So you are definitely getting a "\r\n" after the "Yes" word?

If yes, I wander if it is a problem with the email client that you are testing with not handling the line feed and carriage return characters correctly. It seems very strange that it randomly works.

Instead of using plain text in the email body, how about using html (message.IsBodyHtml = true)? So for example, instead of appending a "\r\n", append "<br />".

Update: If your email client is Outlook, this link might be relevant for you: http://www.emailsignature.eu/phpBB2/outlook-is-stripping-line-breaks-from-plain-text-emails-t1775.html

0

Here is the method that writes the string to the email body

private static string GetEmailBody(PDI bo)
        {

            StringBuilder builder = new StringBuilder();

            builder.Append("Full Houlse?: ");
            builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.FullHoulse));
            builder.AppendLine();
            builder.Append("Any on call?: ");
            builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.Anyoncall));
            builder.AppendLine();
            builder.Append("Phone Number?: ");
            builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.PhoneNumber));
            builder.AppendLine();
            builder.Append("Email Address?: ");
            builder.Append(CommonUtilities.GetYESNOfromBOOL(bo.EmailAddress));
            builder.AppendLine();


            return builder.ToString();
        }

Here is the part that sends the email:

       DistributionListData data = new DistributionListData();
            data.LoadData(-1);
            MailMessage message = new MailMessage();
            message.Subject = "Subject";
            message.Body = GetEmailBody(variable) + "\n";


            message.To.Add(new MailAddress("Name@domain.com"));
            (new SmtpClient()).Send(message);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.