tags:

views:

157

answers:

7

I am trying to send an smtp email from my c# web site. it sends fine all except when a textbox text has line breaks in it it does not format these in the email.. instead it just has one long string of text.

the email is html encoded so how do i make this:

tbDeliver.Text

keep the line breaks in the email?

Thanks in advance

A: 

You could replace \r\n in .Text with <br/> that might work, do you output in a paragraph <p>?

Lloyd
A: 

You'll need to replace \n with <br> in tbDeliver.Text.

string formatted = tbDeliver.Text.Replace ("\n", "<br />");
jball
+4  A: 

Try this:

var textBoxText = tbDeliver.Text.Replace(Environment.NewLine, "<br>");
Sani Huttunen
upvoted for Environment.NewLine
Jack Marchetti
Yeh good call, keep it platform agnostic.
Lloyd
You're making assumptions about the client patform matching the server platform with this. It's not really platform agnostic, it's just based on what the server platform is. If everything was happening on the server then it would be platform agnostic, but for input from a client machine it's just server-centric. An assumption has to be made somewhere, and `\n` seems pretty universal.
jball
A: 

Html encode the linebreaks to '<br/>' or '<p>' tags.

klausbyskov
+2  A: 

make sure that the body format of the message is of type Html. if you are using system.web to send mail :

System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
mail.BodyFormat = Web.Mail.MailFormat.Html;

if you are using system.net :

    System.Net.Mail.MailMessage mail = New System.Net.Mail.MailMessage();
    mail.IsBodyHtml = True;
Microgen
yup using .Net and i have that. Thanks
Andy
+2  A: 

I do this with a regular expression to make sure I get any flavor of newline:

Regex matchNewLine = new Regex("\r\n|\r|\n", RegexOptions.Compiled | RegexOptions.Singleline);
string result = matchNewLine.Replace(originalText, "<br />");
Ray
A: 

ACK!

NEVER EVER EVER use Environment.NewLine to replace <BR> tags in email.

I can't stress this enough.

On some platforms, Environment.NewLine will be \n's, which is a huge no-no in the SMTP world.

In SMTP, all line breaks must be \r\n.

Eventually, your mail will get rejected as spam by some servers using only \n.

always use \r\n or vbCrLf to replace <br> tags.

Cheers!

Dave

dave wanta