C# .Net.Mail新行

C# .Net.Mail新行,c#,mailmessage,C#,Mailmessage,我正在使用.Net.Mail发送短信,但插入新行时遇到问题。我在互联网上(包括StackOverflow)搜索了一种方法来实现这一点。我发现有好几个但都不管用。我正在使用StringBuilder构建消息体,但添加新行或空行会导致在消息中插入新行。我也尝试过“\n”和其他几种方法,但似乎没有任何效果。我在下面添加了我的代码。有人知道我怎么做吗。 提前感谢您提供的任何帮助 string cellPhone = reader.GetString(reader.GetOrdinal("CellPhon

我正在使用.Net.Mail发送短信,但插入新行时遇到问题。我在互联网上(包括StackOverflow)搜索了一种方法来实现这一点。我发现有好几个但都不管用。我正在使用StringBuilder构建消息体,但添加新行或空行会导致在消息中插入新行。我也尝试过“\n”和其他几种方法,但似乎没有任何效果。我在下面添加了我的代码。有人知道我怎么做吗。 提前感谢您提供的任何帮助

string cellPhone = reader.GetString(reader.GetOrdinal("CellPhone"));
string suffix = reader.GetString(reader.GetOrdinal("Suffix"));
StringBuilder body = new StringBuilder();
cellPhone = cellPhone.Trim().Replace(" ", "").Replace("-", "").Replace("(", "").Replace(")", "");
if (!suffix.StartsWith("@"))
{
    suffix = "@" + suffix;
}
string address = cellPhone + suffix;
reader.Close();

EMail email = new EMail("mail.yyy.com");
email.IsHTML = true;
email.Subject = "Sales Lead from yyy.com";
email.Add_ToAddress(address, false);
body.AppendLine(" ");
body.AppendLine("Name: " + this.tbSalesLeadName2.Text + "EMail: mailto: " + this.tbSalesLeadEmail2.Text);

if (!this.chkSalesLeadEmail2.Checked)  //&& (!this.hidCellPhoneProvider.Value.Equals("0", StringComparison.InvariantCultureIgnoreCase)))
{
    body.AppendLine("Phone: " + this.tbSalesLeadPhone2.Text);
    body.AppendLine("Cell Phone: " + this.tbSalesLeadCellPhone.Text);
}

body.AppendLine(" ");
body.AppendLine(" ");
body.AppendLine("Comments: " + this.tbSalesLeadComments2.Text);
body.AppendLine(" ");
body.AppendLine(" ");
body.AppendLine("***To respond start a new text thread with the cell phone number listed above");
email.Body = body.ToString();
email.From = "xxx@yyy.com";
email.Send_Mail();
你说你的身体是HTML格式的,但它不是

您有两个选项可以解决此问题:

  • IsHTML
    设置为false,换行符应该可以工作

  • 将身体格式化为真正的HTML,即对数据使用
    HtmlEncode
    ,对换行符使用


  • 您已指定电子邮件为HTML。在HTML中忽略新行

    要实际渲染换行符,需要使用

    或其他等效工具


    如果您实际上没有任何要呈现的HTML,则只需指定电子邮件不是HTML电子邮件,新行将呈现为新行。

    当您使用电子邮件时。IsHTML=true;,您应该能够在字符串生成器对象中放置
    标记。。代码看起来像

    body.AppendLine(" <br/>");
    body.AppendLine(" <br/>");
    body.AppendLine("Comments: " + this.tbSalesLeadComments2.Text + "<br/>");
    body.AppendLine(" <br/>");
    body.AppendLine(" <br/>");
    body.AppendLine("***To respond start a new text thread with the cell phone number listed above<br/>");
    email.Body = body.ToString();
    
    body.AppendLine(
    ); 正文.附录行(“
    ”); body.AppendLine(“注释:+this.tbsalesleadcoments2.Text+”
    ); 正文.附录行(“
    ”); 正文.附录行(“
    ”); body.AppendLine(“***要响应,请使用上面列出的手机号码启动一个新的文本线程
    ”; email.Body=Body.ToString();
    谢谢您的建议。请忽略我先前的评论。你的建议行得通,我只需要在需要换行的地方加上“\n”。
    我真的很感谢你的帮助。

    Environment.NewLine
    如果使用AppendFormat()方法将其添加到Stringbuilder中,为什么不呢?我相信我会一直使用Environment.NewLine,所以我不确定你为什么说它对Servy没有帮助,我很清楚如果email.IsHTML=true,

    也会起作用\\@DJKRAZE它和仅仅使用新行字符的字符串文本没有什么不同。它会呈现完全相同的字符串。它在上下文中甚至没有意义;电子邮件甚至不一定要在写邮件的机器上呈现;呈现客户端很可能使用不同的新行字符(尽管大多数明智的电子邮件客户端都会适当地呈现任何常见的新行序列)。是的,但什么定义了most或SENSIONAL。。谢谢你的帮助comment@DJKRAZE比如说,我还没有见过一个电子邮件客户端不能使用它不期望的新行序列呈现纯文本电子邮件,但即使你找到了一个,您仍然无法解决此问题,因为读取电子邮件的机器与编写电子邮件的机器不一定是同一台机器。看起来他在该邮件中确实有一些HTML代码。有一个
    mailto
    ,似乎他希望将其呈现为链接。@Servy:啊,是的。但如果没有适当的
    等,这是行不通的。谢谢你的回复。我尝试了这两种建议,不幸的是两者都不起作用。消息出来时看起来完全一样。
    body.AppendLine(" <br/>");
    body.AppendLine(" <br/>");
    body.AppendLine("Comments: " + this.tbSalesLeadComments2.Text + "<br/>");
    body.AppendLine(" <br/>");
    body.AppendLine(" <br/>");
    body.AppendLine("***To respond start a new text thread with the cell phone number listed above<br/>");
    email.Body = body.ToString();