在.NET中的字符串之间放置NewLines

时间:2010-03-01 10:38:20

标签: c#

我使用以下c#

代码通过我的应用程序发送电子邮件
 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;

但我收到的邮件是“sheerazahmedShehzoreHyderabadsheerazHyderabadSindhPakistan03453594552034598750258741sheery_1@hotmail.comsingle”,即合并所有值,我希望textboxt的每个值都在一个单独的新行中,即

Sheeraz Ahmed
Shehzore
Hyderabad 

5 个答案:

答案 0 :(得分:10)

StringBuilder sb = new StringBuilder();

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


myMail.Body = sb.ToString();

答案 1 :(得分:7)

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;

或者更好的是,使用stringbuilder或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();

myMail.Body = String.Format("{0}{1}{2}{1}{3}{1} ... ", TextBox1.Text, Environment.NewLine, txtCName.Text, txtAddress.Text -- etc...

答案 2 :(得分:1)

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;

答案 3 :(得分:0)

将String.Format与Environment.NewLine结合使用。

e.g。

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

您只需在Environment.NewLine的字符串中定义一个占位符,就可以重复使用它。

答案 4 :(得分:0)

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

这会将text1,text2和text3放在一个单独的行中。