vb.net StringBuilder.AppendLine() 不换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16334244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:27:49  来源:igfitidea点击:

StringBuilder.AppendLine() does not make a new line

.netvb.netstringemailstringbuilder

提问by Hyman Pettinger

I am generating an email when an error occurs. I am using StringBuilder.AppendLine()and StringBuilder.AppendLine(String)to build up the body of the email, however the body of my email appears as one very long line with no line breaks.

发生错误时我正在生成电子邮件。我正在使用StringBuilder.AppendLine()StringBuilder.AppendLine(String)来构建电子邮件的正文,但是我的电子邮件正文显示为一行很长的行,没有换行符。

For example:

例如:

Dim ErrorsStringBuilder As New StringBuilder
ErrorsStringBuilder.AppendLine(String.Format("Status : {0}  Message : {1}", Results.Status, Results.ErrorMessage))
ErrorsStringBuilder.AppendLine(String.Format("Failed : {0}  Total : {1}", Results.FailedCount, Results.TotalCount))
ErrorsStringBuilder.AppendLine("Batch #: " & Results.BatchNumber)
ErrorsStringBuilder.AppendLine()
ErrorsStringBuilder.AppendLine("Individual Errors:")
ErrorsStringBuilder.AppendLine()
For Each FailedRecord .......
  ErrorsStringBuilder.AppendLine(String.Format("Failed Record ID : {0}  Message : {1}", FailedRecord.ID, FailedRecord.ErrorText))
Next

This is the description from MSDN for .AppendLine()and .AppendLine(String)respectively.

这是来自 MSDN 的.AppendLine().AppendLine(String)分别的描述。

Appends the default line terminator to the end of the current StringBuilder object.

Appends a copy of the specified string followed by the default line terminator to the end of the current StringBuilder object.

将默认行终止符附加到当前 StringBuilder 对象的末尾。

将指定字符串的副本后跟默认行终止符附加到当前 StringBuilder 对象的末尾。

My question is: whyis there no line break? Whyis the default line terminator not being applied? Am I misunderstanding the description?

我的问题是:为什么没有换行符?为什么没有应用默认的行终止符?我误解了描述吗?

ps. I did look at this questionbut there is no explaination to why.

附:我确实看过这个问题,但没有解释为什么。

and I know that I can use System.Environment.NewLineor "\n"to get linebreaks.

并且我知道我可以使用System.Environment.NewLine"\n"获取换行符。

回答by Parag Meshram

It seems that your email body is of type HTMLbut you are rendering simple text-

似乎您的电子邮件正文属于类型,HTML但您正在渲染simple text-

If you really want it to be simple textthen in your email object set IsBodyHtml = falseit will render new lines without modifying your StringBuilder code -

如果你真的希望它simple text在你的电子邮件对象集中,IsBodyHtml = false它会在不修改你的 StringBuilder 代码的情况下呈现新行 -

MailMessage mail = new MailMessage();
mail.IsBodyHtml = false;

If you want HTML only then try adding after every line -

如果您只想要 HTML,请尝试在每一行之后添加 -

ErrorsStringBuilder.AppendLine("<br />");

Actually you are trying to build HTML output.

实际上,您正在尝试构建 HTML 输出。

In HTML if you write -

在 HTML 中,如果你写 -

<html>
<body>
Hi,

How are you?
</body>
</html>

It would result in -

这会导致——

Hi, How are you? 

To make it work, you should enclose these two statements in to two different block elements -

为了使其工作,您应该将这两个语句包含在两个不同的块元素中 -

In this case I am using paragraph -

在这种情况下,我使用的是段落 -

<html>
<body>
<p>Hi,</p>

<p>How are you?</p>
</body>
</html>

now this will result in -

现在这将导致 -

Hi,

How are you?

In your case you need to figure out HTML to render required output. Otherwise use <br />as a workaround.

在您的情况下,您需要找出 HTML 来呈现所需的输出。否则<br />用作解决方法。

Example for HTML with <p>tag -

带有<p>标签的HTML 示例-

Dim ErrorsStringBuilder As New StringBuilder
ErrorsStringBuilder.AppendLine(String.Format("<p>Status : {0}  Message : {1}</p>", Results.Status, Results.ErrorMessage))
ErrorsStringBuilder.AppendLine(String.Format("<p>Failed : {0}  Total : {1}</p>", Results.FailedCount, Results.TotalCount))
ErrorsStringBuilder.AppendLine("<p>Batch #: </p>" & Results.BatchNumber)
ErrorsStringBuilder.AppendLine()
ErrorsStringBuilder.AppendLine("<p>Individual Errors:</p>")
ErrorsStringBuilder.AppendLine()
For Each FailedRecord .......
  ErrorsStringBuilder.AppendLine(String.Format("<p>Failed Record ID : {0}  Message : {1}</p>", FailedRecord.ID, FailedRecord.ErrorText))
Next

回答by Kna?is

Since it appears as one long line your e-mail message is most probably set to display as HTML, not plain text. In HTML you have to use <br/>for line breaks.

由于它显示为一长行,您的电子邮件消息很可能设置为显示为 HTML,而不是纯文本。在 HTML 中,您必须使用<br/>换行符。

You could try ErrorsStringBuilder.Replace(Environment.NewLine, Environment.NewLine + "<br/>")at the end of your code to add the HTML line breaks.

您可以尝试ErrorsStringBuilder.Replace(Environment.NewLine, Environment.NewLine + "<br/>")在代码末尾添加 HTML 换行符。

回答by ajakblackgoat

If you want to use HTML format for the email body, wrap your ErrorsStringBuilder output inside <pre>...</pre>tags to preserves both spaces and line-breaks and use fixed width fonts to maintain formatting.

如果您想对电子邮件正文使用 HTML 格式,请将您的 ErrorsStringBuilder 输出包装在<pre>...</pre>标签内以保留空格和换行符,并使用固定宽度的字体来保持格式。