如何设置电子邮件中字符串的格式以便 Outlook 打印换行符?

发布于 2024-07-06 08:15:31 字数 224 浏览 8 评论 0原文

我正在尝试用 Java 发送电子邮件,但是当我在 Outlook 中阅读电子邮件正文时,它删除了我所有的换行符。 我将 \n 放在行尾,但除此之外我还需要做一些特别的事情吗? 收件人始终使用 Outlook。

我在 microsoft.com 上发现一个页面,显示 Outlook 中有一个“删除换行符”“功能”,这是否意味着除了取消选中该设置之外没有其他解决方案可以解决该问题?

谢谢

I'm trying to send an email in Java but when I read the body of the email in Outlook, it's gotten rid of all my linebreaks. I'm putting \n at the ends of the lines but is there something special I need to do other than that? The receivers are always going to be using Outlook.

I found a page on microsoft.com that says there's a 'Remove line breaks' "feature" in Outlook so does this mean there's no solution to get around that other than un-checking that setting?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

ら栖息 2024-07-13 08:15:31

我今天刚刚为此而奋斗。 我们将删除多余换行符的行为称为“延续”。 一些实验发现以下行为:

  • 每条消息都以继续关闭开始。
  • 长度小于 40 个字符的行不会触发延续,但如果启用延续,则会删除它们的换行符。
  • 40 个字符或更长的行将启用延续。 它会一直保持打开状态,直到发生将其关闭的事件为止。
  • 以句号、问号、感叹号或冒号结尾的行会关闭延续。 (Outlook 假定它是句子的结尾?)
  • 关闭延续的行将以换行符开头,但如果长度超过 40 个字符,则会重新打开延续。
  • 以制表符开始或结束的行会关闭连续性。
  • 以 2 个或更多空格开头的行将关闭连续性。
  • 以 3 个或更多空格结尾的行将关闭延续。

请注意,我在 Outlook 2007 中尝试了所有这些操作。YMMV。
因此,如果可能的话,请以句子结束标点符号、制表符甚至三个空格结束所有项目符号项目。

I've just been fighting with this today. Let's call the behavior of removing the extra line breaks "continuation." A little experimenting finds the following behavior:

  • Every message starts with continuation off.
  • Lines less than 40 characters long do not trigger continuation, but if continuation is on, they will have their line breaks removed.
  • Lines 40 characters or longer turn continuation on. It remains on until an event occurs to turn it off.
  • Lines that end with a period, question mark, exclamation point or colon turn continuation off. (Outlook assumes it's the end of a sentence?)
  • Lines that turn continuation off will start with a line break, but will turn continuation back on if they are longer than 40 characters.
  • Lines that start or end with a tab turn continuation off.
  • Lines that start with 2 or more spaces turn continuation off.
  • Lines that end with 3 or more spaces turn continuation off.

Please note that I tried all of this with Outlook 2007. YMMV.
So if possible, end all bullet items with a sentence-terminating punctuation mark, a tab, or even three spaces.

流云如水 2024-07-13 08:15:31

在换行符 (CRLF) 之前附加一个(或两个?)制表符 (\t) 时,您可以在 Outlook 中强制换行。

示例:

This is my heading in the mail\t\n
Just here Outlook is forced to begin a new line.

它似乎适用于 Outlook 2010。请测试这是否适用于其他版本。

另请参阅 Outlook 自动清理我的线路破坏并搞乱了我的电子邮件格式

You can force a line break in outlook when attaching one (or two?) tab characters (\t) just before the line break (CRLF).

Example:

This is my heading in the mail\t\n
Just here Outlook is forced to begin a new line.

It seems to work on Outlook 2010. Please test if this works on other versions.

See also Outlook autocleaning my line breaks and screwing up my email format

虐人心 2024-07-13 08:15:31

您需要使用 \r\n 作为解决方案。

You need to use \r\n as a solution.

水溶 2024-07-13 08:15:31

默认情况下,Microsoft Outlook 2002 及更高版本会从短信中删除“额外换行符”(kb308319)。 也就是说,Outlook 似乎只是忽略文本消息中的换行符和/或回车符序列,将所有行放在一起。

如果您尝试编写自动生成电子邮件供使用 Outlook 的人阅读的代码,这可能会导致问题。

例如,为了清晰起见,假设您希望在不同的行中提供单独的信息,如下所示:

交易需要注意!
发布日期:2009 年 1 月 30 日
金额:12,222.06 美元
转运ID:8gk288g229g2kg89
邮政编码:91543

您的 Outlook 收件人将看到所有信息都被粉碎在一起,如下:

交易需要注意! 发布日期:2009 年 1 月 30 日 金额:12,222.06 美元 TransID:8gk288g229g2kg89 邮政编码:91543

似乎没有一个简单的解决方案。 替代方案是:

  1. 您可以在每行之间提供两组换行符。 这确实会阻止 Outlook 将行合并到一行,但它会在每行之间显示一个额外的空行(产生相反的问题)。 通过“提供两组换行符”,我的意思是您应该使用“\r\n\r\n”或“\r\r”或“\n\n”,但不使用“\r\n”或“\n” \r"。
  2. 您可以在电子邮件正文中每行的开头提供两个空格。 这避免了在每行之间引入额外的空行。 但是,如果邮件中的每一行都相当短,则此方法效果最佳,因为用户可能会在非常窄的 Outlook 窗口中预览文本,该窗口会将每行的末尾环绕到下一行的第一个位置,而在该窗口中不会出现这种情况。与两个空格的缩进线对齐。 此策略已用于某些新闻通讯
  3. 您可以放弃使用纯文本格式,而使用 html 格式。

Microsoft Outlook 2002 and above removes "extra line breaks" from text messages by default (kb308319). That is, Outlook seems to simply ignore line feed and/or carriage return sequences in text messages, running all of the lines together.

This can cause problems if you're trying to write code that will automatically generate an email message to be read by someone using Outlook.

For example, suppose you want to supply separate pieces of information each on separate lines for clarity, like this:

Transaction needs attention!

PostedDate: 1/30/2009

Amount: $12,222.06

TransID: 8gk288g229g2kg89

PostalCode: 91543

Your Outlook recipient will see the information all smashed together, as follows:

Transaction needs attention! PostedDate: 1/30/2009 Amount: $12,222.06 TransID: 8gk288g229g2kg89 ZipCode: 91543

There doesn't seem to be an easy solution. Alternatives are:

  1. You can supply two sets of line breaks between each line. That does stop Outlook from combining the lines onto one line, but it then displays an extra blank line between each line (creating the opposite problem). By "supply two sets of line breaks" I mean you should use "\r\n\r\n" or "\r\r" or "\n\n" but not "\r\n" or "\n\r".
  2. You can supply two spaces at the beginning of every line in the body of your email message. That avoids introducing an extra blank line between each line. But this works best if each line in your message is fairly short, because the user may be previewing the text in a very narrow Outlook window that wraps the end of each line around to the first position on the next line, where it won't line up with your two-space-indented lines. This strategy has been used for some newsletters.
  3. You can give up on using a plain text format, and use an html format.
铃予 2024-07-13 08:15:31

我有同样的问题,并找到了解决方案。 试试这个:%0D%0A 添加换行符。

I had the same issue, and found a solution. Try this: %0D%0A to add a line break.

倾城花音 2024-07-13 08:15:31

我使用了 html 换行符而不是 "\n" 。 效果很好。

I have used html line break instead of "\n" . It worked fine.

挥剑断情 2024-07-13 08:15:31

添加“\t\r\n”(\t 代表 TAB)而不是“\r\n”在 Outlook 2010 上对我有用。 注意:在每行末尾添加 3 个空格也可以做同样的事情,但这看起来像是编程技巧!

Adding "\t\r\n" ( \t for TAB) instead of "\r\n" worked for me on Outlook 2010 . Note : adding 3 spaces at end of each line also do same thing but that looks like a programming hack!

書生途 2024-07-13 08:15:31

您需要发送 HTML 电子邮件。 在电子邮件中使用
,您将始终有换行符。

You need to send HTML emails. With <br />s in the email, you will always have your line breaks.

风启觞 2024-07-13 08:15:31

我一直在努力解决上述所有解决方案,但这里没有任何帮助,因为我在我的电子邮件库中使用了字符串变量(来自 JTextPane 的纯文本)与“text/html”格式的组合。

因此,解决这个问题的方法是使用“text/plain”,而不是“text/html”,并且根本不需要替换返回字符:

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/plain");

I had been struggling with all of the above solutions and nothing helped here, because I used a String variable (plain text from a JTextPane) in combination with "text/html" formatting in my e-mail library.

So, the solution to this problem is to use "text/plain", instead of "text/html" and no need to replace return characters at all:

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/plain");
幽梦紫曦~ 2024-07-13 08:15:31

技巧是使用 js 中的 encodeURIComponent() 功能:

var formattedBody = "FirstLine \n Second Line \n Third Line";
var mailToLink = "mailto:[email protected]?body=" + encodeURIComponent(formattedBody);

结果:

FirstLine
SecondLine
ThirdLine

The trick is to use the encodeURIComponent() functionality from js:

var formattedBody = "FirstLine \n Second Line \n Third Line";
var mailToLink = "mailto:[email protected]?body=" + encodeURIComponent(formattedBody);

RESULT:

FirstLine
SecondLine
ThirdLine
落叶缤纷 2024-07-13 08:15:31

对于 Outlook 2010 及更高版本,请使用 \t\n 而不是使用 \r\n。

For Outlook 2010 and later versions, use \t\n rather than using \r\n.

◇流星雨 2024-07-13 08:15:31

如果可以添加“.” (点)字符位于每行末尾,这似乎可以防止 Outlook 破坏文本格式。

If you can add in a '.' (dot) character at the end of each line, this seems to prevent Outlook ruining text formatting.

掀纱窥君容 2024-07-13 08:15:31

尝试使用 \r\c 而不是 \n

编辑:我认为@Robert Wilkinson 说得对。 <代码>\r\n。 记忆已经不再是以前的样子了。

Try \r\c instead of \n.

EDIT: I think @Robert Wilkinson had it right. \r\n. Memory just isn't what it used to be.

屋檐 2024-07-13 08:15:31

\n 很大程度上适合我们,但 Outlook 有时确实会像您所说的那样自行删除换行符。

The \n largely works for us, but Outlook does sometimes take it upon itself to remove the line breaks as you say.

别低头,皇冠会掉 2024-07-13 08:15:31

我也遇到了纯文本/文本邮件类型的问题。 早些时候,我使用了“\n\n”,但有两个换行符。 然后,我使用“\t\n”并且它起作用了。 我在 java 中使用 StringBuffer 来附加内容。
内容在 Outlook 2010 邮件的下一行中打印。

I also had this issue with plain/text mail type. Earlier, I used "\n\n" but there was two line breaks. Then, I used "\t\n" and it worked. I was using StringBuffer in java to append content.
The content got printed in next line in Outlook 2010 mail.

给不了的爱 2024-07-13 08:15:31

将文本放入

 标签中,outlook 将正确格式化并显示文本。

我在 HTML Body 的 CSS 内联中定义了它,如下所示:

CSS:

pre {
 font-family: Verdana, Geneva, sans-serif;
}

我定义了必须设置字体的字体系列。

HTML:

<td width="70%"><pre>Entry Date/Time:       2013-09-19 17:06:25
Entered By:     Chris

worklog mania

____________________________________________________________________________________________________

Entry Date/Time:        2013-09-19 17:05:42
Entered By:     Chris

this is a new Worklog Entry</pre></td>

Put the text in <pre> Tags and outlook will format and display the text correctly.

i defined it in CSS inline in HTML Body like:

CSS:

pre {
 font-family: Verdana, Geneva, sans-serif;
}

i defined the font-family to have to font set.

HTML:

<td width="70%"><pre>Entry Date/Time:       2013-09-19 17:06:25
Entered By:     Chris

worklog mania

____________________________________________________________________________________________________

Entry Date/Time:        2013-09-19 17:05:42
Entered By:     Chris

this is a new Worklog Entry</pre></td>
剧终人散尽 2024-07-13 08:15:31

因为它是一个查询,所以只有百分比转义字符有效,这意味着 %0A 会给您一个换行符。 例如,

<a href="mailto:[email protected]?Subject=TEST&amp;body=Hi there,%0A%0AHow are you?%0A%0AThanks">email to me</a>

Because it is a query, only percent escaped characters work, means %0A gives you a line break. For example,

<a href="mailto:[email protected]?Subject=TEST&amp;body=Hi there,%0A%0AHow are you?%0A%0AThanks">email to me</a>
ゞ花落谁相伴 2024-07-13 08:15:31

我也遇到了纯/文本邮件类型的问题。Form Feed \f 对我有用。

I also had this issue with plain/text mail type.Form Feed \f worked for me.

幻梦 2024-07-13 08:15:31

有时您必须输入 \r\n 两次才能强制 Outlook 进行中断。

这将添加一个空行,但所有行都会中断。

Sometimes you have to enter \r\n twice to force outlook to do the break.

This will add one empty line but all the lines will have break.

女皇必胜 2024-07-13 08:15:31

在将正文类型设置为文本之前,\r\n 将不起作用。

message.setBody(MessageBody.getMessageBodyFromText(msg));
BodyType type = BodyType.Text;
message.getBody().setBodyType(type);

\r\n will not work until you set body type as text.

message.setBody(MessageBody.getMessageBodyFromText(msg));
BodyType type = BodyType.Text;
message.getBody().setBodyType(type);
客…行舟 2024-07-13 08:15:31

我遇到了同样的问题,这是解决该问题的代码:

\t\n - for new line in Email service JavaMailSender

String mailMessage = JSONObject.toJSONString("Your message").replace(",", "\t\n").trim();

I was facing the same issue and here is the code that resolved it:

\t\n - for new line in Email service JavaMailSender

String mailMessage = JSONObject.toJSONString("Your message").replace(",", "\t\n").trim();
诺曦 2024-07-13 08:15:31

在我的应用程序中已解决

在我的应用程序中,我试图发送一封电子邮件,其邮件正文由用户在文本区域中键入。 发送邮件时,Outlook 自动删除用户输入的换行符。

例如,如果用户输入
亚达夫
Mahesh

Outlook 将其显示为
YadavMahesh

解决方案:我将换行符“\r\n”更改为“\par”(记住在 RTF 代码“\par”末尾按空格)并且换行符被恢复。

干杯,
马赫什

RESOLVED IN MY APPLICATION

In my application, I was trying to send an email whose message body was typed by the user in text area. When mail was send, outlook automatically removed line break entered by user.

e.g if user entered
Yadav
Mahesh

outlook displayed it as
YadavMahesh

Resolution: I changed the line break character "\r\n" with "\par " ( remember to hit space at the end of RTF code "\par" )and line breaks are restrored.

Cheers,
Mahesh

是伱的 2024-07-13 08:15:31

试试这个:

message.setContent(new String(body.getBytes(), "iso-8859-1"),
                    "text/html; charset=\"iso-8859-1\"");

问候,
穆罕默德·拉苏尔·贾维德

Try this:

message.setContent(new String(body.getBytes(), "iso-8859-1"),
                    "text/html; charset=\"iso-8859-1\"");

Regards,
Mohammad Rasool Javeed

旧人九事 2024-07-13 08:15:31

我尝试过一个很好的解决方案,只需在行尾添加 Char(13) 即可,如下例所示:

Dim S As String
 S = "Some Text" & Chr(13)
 S = S + "Some Text" & Chr(13)

I have a good solution that I tried it, it is just add the Char(13) at end of line like the following example:

Dim S As String
 S = "Some Text" & Chr(13)
 S = S + "Some Text" & Chr(13)
如梦亦如幻 2024-07-13 08:15:31

如果消息是文本/纯文本,则 \r\n 应该可以工作;
如果消息类型是text\html,则使用< p>>

if the message is text/plain using, \r\n should work;
if the message type is text\html, use < p/>

橪书 2024-07-13 08:15:31

如果工作需要使用没有 html 编码的格式化文本来完成。
通过以下场景可以轻松实现 动态创建 div 元素 并使用 < code>

html 元素以保持格式设置。

var email_body = htmlEncode($("#Body").val());

 function htmlEncode(value) {
    return "<pre>" + $('<div/>').text(value).html() + "</pre>";
}

if work need to be done with formatted text with out html encoding.
it can be easy achieved with following scenario that creates div element on the fly and using <pre></pre> html element to keep formatting.

var email_body = htmlEncode($("#Body").val());

 function htmlEncode(value) {
    return "<pre>" + $('<div/>').text(value).html() + "</pre>";
}
还如梦归 2024-07-13 08:15:31

不确定上面是否提到过,但 Outlook 有一个名为“删除纯文本消息中的额外换行符”的复选框设置,并且默认情况下处于选中状态。 对于不同版本的 Outlook,它位于不同的位置,但对于 2010,请转到“文件”选项卡。 选择“选项=>邮件”向下滚动到“邮件格式”取消选中该复选框。

Not sure if it was mentioned above but Outlook has a checkbox setting called "Remove extra line breaks in plain text messages" and is checked by default. It is located in a different spot for different versions of Outlook but for 2010 go to the "File" tab. Select "Options => Mail" Scroll down to "Message format" Uncheck the checkbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文