Placing NewLines between lines in .NET.

I am sending email through my application using the following C # code

 myMail.Body = TextBox1.Text+
                      txtName.Text+
                      txtCName.Text+
                      txtAddress.Text+
                      TextBox1.Text+
                      txtCity.Text+
                      txtState.Text+
                      txtCountry.Text+
                      txtPhone.Text+
                      Fax.Text+
                      txtCell.Text+
                      txtEmail.Text+
                      txtPrinting.Text;
        myMail.BodyEncoding = System.Text.Encoding.UTF8;

but I get mail in this form "sheerazahmedShehzoreHyderabad sheerazHyderabadSindhPakistan03453594552034598750258741sheery_1 @ hotmail.comsingle" ie merge all values, I want each textboxt value in a separate new line ie

Sheeraz Ahmed
Shehzore
Hyderabad 

and etc.

+3
source share
5 answers
myMail.Body = TextBox1.Text + Environment.NewLine + 
                  txtName.Text+ Environment.NewLine + 
                  txtCName.Text+ Environment.NewLine + 
                  txtAddress.Text+ Environment.NewLine + 
                  TextBox1.Text+ Environment.NewLine + 
                  txtCity.Text+ Environment.NewLine + 
                  txtState.Text+ Environment.NewLine + 
                  txtCountry.Text+ Environment.NewLine + 
                  txtPhone.Text+ Environment.NewLine + 
                  Fax.Text+ Environment.NewLine + 
                  txtCell.Text+ Environment.NewLine + 
                  txtEmail.Text+ Environment.NewLine + 
                  txtPrinting.Text;
    myMail.BodyEncoding = System.Text.Encoding.UTF8;

Or better yet, use stringbuilder or string.Format

StringBuilder bodyBuilder = new StringBuilder("");
bodyBuilder .AppendLine(TextBox1.Text);
bodyBuilder .AppendLine(txtName.Text);
bodyBuilder .AppendLine(txtCName.Text);
bodyBuilder .AppendLine(txtAddress.Text);
  // etc.
myMail.Body = bodyBuilder .ToString();

or

myMail.Body = String.Format("{0}{1}{2}{1}{3}{1} ... ", TextBox1.Text, Environment.NewLine, txtCName.Text, txtAddress.Text -- etc...
+7
source
StringBuilder sb = new StringBuilder();

sb.AppendLine(TextBox1.Text);
sb.AppendLine(txtName.Text);
...


myMail.Body = sb.ToString();
+10
source
myMail.Body = TextBox1.Text+ Environment.NewLine +
                      txtName.Text+ Environment.NewLine +
                      txtCName.Text+ Environment.NewLine +
                      txtAddress.Text+ Environment.NewLine +
                      TextBox1.Text+ Environment.NewLine +
                      txtCity.Text+ Environment.NewLine +
                      txtState.Text+ Environment.NewLine +
                      txtCountry.Text+ Environment.NewLine +
                      txtPhone.Text+ Environment.NewLine +
                      Fax.Text+ Environment.NewLine +
                      txtCell.Text+ Environment.NewLine +
                      txtEmail.Text+ Environment.NewLine +
                      txtPrinting.Text;
+1

String.Format Environment.NewLine.

.

String.Format("{0}{1}{2}{1}{3}", "ValueOfTextbox1", Environment.NewLine, "ValueOfTextbox2", "ValueOfTextbox3");

You only need to specify one placeholder per line for Environment.NewLine, and you can reuse it.

0
source
result = text1 + Environment.NewLine + text2 + Environment.NewLine + text3;

this will put text1, text2 and text3 on a separate line.

0
source

Source: https://habr.com/ru/post/1734855/


All Articles