Monday, March 26, 2012

StringBuilder and objMail

I am new to ASP.NET so please be gentle :-)
I am trying to build an text (not HTML) email. I am putting together the
string that will be the body of the email but the formatting keeps getting
messed up. See code below.

Thanks in advance for any help you can give!

I want it to look like this...

Thank you for ordering from mycompany.com!

Your order is being billed to:
John Smith

******** But it keeps coming out like **************

Thank you for ordering from mycompany.com!Your order is being billed to: "
John Smith

******* Here is my code ********
Dim sEmail As New System.Text.StringBuilder

sEmail.Append("Thank you for ordering from mycompany.com!" & Chr(13))
sEmail.Append("Your order is being billed to: " & Chr(34))
sEmail.Append(Chr(32) & rdrMbrs.Item("fullname"))

objMail.Body = sEmail.toString()

Quote:

Originally Posted by

******** But it keeps coming out like **************
>
Thank you for ordering from mycompany.com!Your order is being billed to: "
John Smith
>
>
******* Here is my code ********
sEmail.Append("Thank you for ordering from mycompany.com!" & Chr(13))
sEmail.Append("Your order is being billed to: " & Chr(34))
sEmail.Append(Chr(32) & rdrMbrs.Item("fullname"))


The newline in the internet world is not Chr(13) which is only
carriage-return.

The newline if CRLF:
Chr(13) & Chr(10)

I'm not sure but I think vbCrlf - constant - should be automatically
available to VB.Net programmers. It used to be there in VB6 and I think
should exist even now.

See this page for a good discussion:
http://en.wikipedia.org/wiki/Newline
HTH

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------
I agree with Gaurav. However, a better approach is to use
System.Environment.NewLine.

Regards,
Walter Wang (wawang@.online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
>I agree with Gaurav. However, a better approach is to use

Quote:

Originally Posted by

System.Environment.NewLine.


Not sure if System.Environment.NewLine is a good approach since again, we
are going to OS specific newline which is CRLF on Windows, CR on Mac and LF
on Unix/Linux etc.

Ok... if we are working with Microsoft .Net Framework, we know that we are
on Windows and it will translate to CRLF.

But I would like to play safe. Although an impossible probability today, but
what if Microsoft plans to release it on other OS as well ;)

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------
Hi Gaurav,

Using System.Environment.NewLine is the preferred way to represent a
"NewLine"; although you're right that the internal implementation of this
read-only property currently is implemented as:

public static string NewLine
{
get
{
return "\r\n";
}
}

However, consider someday your code is going to run on other platform, your
code doesn't have to be modified since you're calling into the BCL (Base
Class Library). I believe the BCL on different platform should return the
new line constant accordingly.

#Brad Abrams : Pet Peeve #493: Console.WriteLine ("\n")
http://blogs.msdn.com/brada/archive.../08/211053.aspx
Regards,
Walter Wang (wawang@.online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Quote:

Originally Posted by

However, consider someday your code is going to run on other platform,
your
code doesn't have to be modified since you're calling into the BCL (Base
Class Library). I believe the BCL on different platform should return the
new line constant accordingly.


I agree. I use it as the way to write a new-line (mainly since I also work
with Mono at times).

But hey! Hold on... the original problem was in the mail-message being sent.
In the mail-message composed, Environment.NewLine is definitely a very bad
choice.
There cannot be any choice other than CRLF (="\r\n" or "Chr(13) & Chr(10)").

I still hold on to my point that Environment.NewLine should not be used to
indicate a new-line while composing a mail.

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------
This worked great!!! Chr(13) & Chr(10) Thanks everyone for all you help.
One more thing why does the tab also not work? Chr(32)

Thanks Again!

"Gaurav Vaish (MasterGaurav)" <gaurav.vaish.nospam@.nospam.gmail.comwrote
in message news:%2374qm4VQHHA.3444@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

>

Quote:

Originally Posted by

>However, consider someday your code is going to run on other platform,
>your
>code doesn't have to be modified since you're calling into the BCL (Base
>Class Library). I believe the BCL on different platform should return the
>new line constant accordingly.


>
I agree. I use it as the way to write a new-line (mainly since I also work
with Mono at times).
>
But hey! Hold on... the original problem was in the mail-message being
sent.
In the mail-message composed, Environment.NewLine is definitely a very bad
choice.
There cannot be any choice other than CRLF (="\r\n" or "Chr(13) &
Chr(10)").
>
I still hold on to my point that Environment.NewLine should not be used to
indicate a new-line while composing a mail.
>
>
>
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujinionline.com
-------------
>
>


"Rick" <rick@.di-wave.comwrote in message
news:OuKqUIWQHHA.496@.TK2MSFTNGP06.phx.gbl...

Quote:

Originally Posted by

This worked great!!! Chr(13) & Chr(10) Thanks everyone for all you help.
One more thing why does the tab also not work? Chr(32)


Chr(32) is a <space>
Chr(9) is a <tab>
Thanks very much!!

"Mark Rae" <mark@.markNOSPAMrae.comwrote in message
news:e%23E1InWQHHA.5032@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

"Rick" <rick@.di-wave.comwrote in message
news:OuKqUIWQHHA.496@.TK2MSFTNGP06.phx.gbl...
>

Quote:

Originally Posted by

>This worked great!!! Chr(13) & Chr(10) Thanks everyone for all you help.
>One more thing why does the tab also not work? Chr(32)


>
Chr(32) is a <space>
Chr(9) is a <tab>
>


My apology for ignoring the requirement is to use the line breaks in email
message. According to RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt), the
Line Break in a text body must use a CRLF sequence.

I hope I didn't cause too much confusion.

Regards,
Walter Wang (wawang@.online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
My apology for ignoring the requirement is to use the line breaks in email

Quote:

Originally Posted by

message. According to RFC 2045 (http://www.ietf.org/rfc/rfc2045.txt), the
Line Break in a text body must use a CRLF sequence.


That's ok.
btw, that's not just an RFC-2045 requirement but also elsewhere like in HTTP
etc.
CRLF is the "NewLine" of the "Internet" and specifically, "the Web".

Quote:

Originally Posted by

I hope I didn't cause too much confusion.


I hope not. :)

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-------------

0 comments:

Post a Comment