469,960 Members | 2,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,960 developers. It's quick & easy.

StringBuilder

Hello,

I am creating the body of a mail message to be sent using
StringBuilder:

StringBuilder body = new StringBuilder();
body.AppendFormat("{0},", userName);
body.AppendLine();
body.AppendLine();
body.AppendLine("How are you?");

How should I create empty lines? User various ApendLine as n the
previous example or maybe using <br />:

StringBuilder body = new StringBuilder();
body.AppendFormat("{0},<br/><br/>", userName);
body.AppendLine("How are you?");

or maybe some other way?

Thanks,

Miguel
Nov 6 '08 #1
3 11259
On Thu, 06 Nov 2008 15:11:09 -0800, shapper <md*****@gmail.comwrote:
[...]
How should I create empty lines? User various ApendLine as n the
previous example or maybe using <br />:

StringBuilder body = new StringBuilder();
body.AppendFormat("{0},<br/><br/>", userName);
body.AppendLine("How are you?");
I don't understand the above example. .NET doesn't do anything special
with HTML elements. So appending "<br/>" doesn't insert line-breaks into
your string.

AppendLine() should be fine, or you can append the Environment.NewLine
character.

Pete
Nov 7 '08 #2
On Nov 7, 12:59*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 06 Nov 2008 15:11:09 -0800, shapper <mdmo...@gmail.comwrote:
[...]
How should I create empty lines? User various ApendLine as n the
previous example or maybe using <br />:
* * StringBuilder body = new StringBuilder();
* * body.AppendFormat("{0},<br/><br/>", userName);
* * body.AppendLine("How are you?");

I don't understand the above example. *.NET doesn't do anything special*
with HTML elements. *So appending "<br/>" doesn't insert line-breaks into *
your string.

AppendLine() should be fine, or you can append the Environment.NewLine *
character.

Pete
But if I am sending the email Html format won't I get the line breaks
by using <br/in my string?
Basically, at the end I do: myMail.Body = body.ToString();

I've also read the following:
http://www.systemnetmail.com/faq/4.8.aspx

So I think I have two options:
1. Create an HTML page that contains the email body and send it ...
I think this would be more useful for complex mail bodies like
Newsletters.
2. Use StringBuilder to create the email body and include the needed
HTML tags as <p>, <br/>, etc ...

And if I send a No Html format email I could use:
body.AppendFormat("{0},\n\n", userName);

instead of:
body.AppendFormat("{0},<br/><br/>", userName);

Or not?

Just trying to find the correct way to create the body of Html mail
messages and Non Html mail messages.

Thanks,
Miguel

Nov 7 '08 #3
On Thu, 06 Nov 2008 17:22:51 -0800, shapper <md*****@gmail.comwrote:
>AppendLine() should be fine, or you can append the Environment.NewLine *
character.

But if I am sending the email Html format won't I get the line breaks
by using <br/in my string?
No. You'll get some text that an HTML display engine will render as
line-breaks. There's a big difference, especially since in HTML, actual
line-breaks _don't_ render as line-breaks (they are considered white-space
and are ignored).
Basically, at the end I do: myMail.Body = body.ToString();

I've also read the following:
http://www.systemnetmail.com/faq/4.8.aspx

So I think I have two options:
1. Create an HTML page that contains the email body and send it ...
I think this would be more useful for complex mail bodies like
Newsletters.
2. Use StringBuilder to create the email body and include the needed
HTML tags as <p>, <br/>, etc ...
If you are trying to send HTML-formatted email, then you should use HTML
codes for line-breaks. How you generate them is unimportant, as long as
you ultimately get the formatting you want.
And if I send a No Html format email I could use:
body.AppendFormat("{0},\n\n", userName);

instead of:
body.AppendFormat("{0},<br/><br/>", userName);

Or not?
You definitely would not want to use HTML codes for line-breaks in
non-HTML email. Whether '\n' is the appropriate line-break character
depends a bit on your recipient, but it would probably work. For
plain-text, non-HTML email, the canonical .NET method is to use
Environment.NewLine or to simply call StringBuilder.AppendLine(). Both of
which I already mentioned.

Pete
Nov 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Alvin Bruney | last post: by
2 posts views Thread by Peter | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
5 posts views Thread by pantagruel | last post: by
reply views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.