Java 如何格式化电子邮件中的字符串以便 Outlook 打印换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/136052/
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
How do I format a String in an email so Outlook will print the line breaks?
提问by MattGrommes
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.
我正在尝试用 Java 发送电子邮件,但是当我在 Outlook 中阅读电子邮件正文时,它摆脱了我所有的换行符。我将 \n 放在行尾,但除此之外我还需要做什么特别的事情吗?接收者将始终使用 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?
我在 microsoft.com 上找到了一个页面,上面写着 Outlook 中有一个“删除换行符”“功能”,这是否意味着除了取消选中该设置之外,没有其他解决方案可以解决这个问题?
Thanks
谢谢
采纳答案by Robert Wilkinson
You need to use \r\n
as a solution.
您需要\r\n
用作解决方案。
回答by EBGreen
Try \r\c
instead of \n
.
尝试\r\c
代替\n
.
EDIT:I think @Robert Wilkinson had it right. \r\n
. Memory just isn't what it used to be.
编辑:我认为@Robert Wilkinson 说得对。\r\n
. 记忆力已经不是以前的样子了。
回答by cagcowboy
The \n
largely works for us, but Outlook does sometimes take it upon itself to remove the line breaks as you say.
这\n
在很大程度上对我们有用,但 Outlook 有时会自行删除您所说的换行符。
回答by hova
You need to send HTML emails. With <br />
s in the email, you will always have your line breaks.
您需要发送 HTML 电子邮件。<br />
在电子邮件中使用s,您将始终有换行符。
回答by user60849
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.
默认情况下,Microsoft Outlook 2002 及更高版本会从文本消息中删除“额外的换行符” (kb308319)。也就是说,Outlook 似乎只是忽略文本消息中的换行和/或回车序列,将所有行一起运行。
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.
如果您尝试编写自动生成电子邮件以供使用 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
交易需注意!
发布日期:1/30/2009
金额:12,222.06 美元
TransID:8gk288g229g2kg89 邮政编码
:91543
Your Outlook recipient will see the information all smashed together, as follows:
您的 Outlook 收件人将看到信息全部拼凑在一起,如下所示:
Transaction needs attention! PostedDate: 1/30/2009 Amount: $12,222.06 TransID: 8gk288g229g2kg89 ZipCode: 91543
交易需注意!发布日期:1/30/2009 金额:12,222.06 美元 TransID:8gk288g229g2kg89 邮编:91543
There doesn't seem to be an easy solution. Alternatives are:
似乎没有一个简单的解决方案。替代方案是:
- You can supply twosets 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".
- 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.
- You can give up on using a plain text format, and use an html format.
- 您可以提供2组,每组线之间的换行符。这确实会阻止 Outlook 将这些行合并为一行,但随后会在每行之间显示一个额外的空行(产生相反的问题)。通过“提供两组换行符”我的意思是你应该使用 "\r\n\r\n" 或 "\r\r" 或 "\n\n" 而不是 "\r\n" 或 "\n \r”。
- 您可以在电子邮件正文的每一行的开头提供两个空格。这避免了在每行之间引入额外的空行。但是,如果您的邮件中的每一行都相当短,这种方法效果最好,因为用户可能正在一个非常窄的 Outlook 窗口中预览文本,该窗口将每一行的末尾环绕到下一行的第一个位置,在那里它不会与您的两个空格缩进的行对齐。这种策略已被用于一些时事通讯。
- 您可以放弃使用纯文本格式,而使用 html 格式。
回答by mtruesdell
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.
- 每条消息都以 continuation off 开始。
- 长度少于 40 个字符的行不会触发续行,但如果启用续行,它们的换行符将被删除。
- 40 个字符或更长的行打开继续。它一直保持打开状态,直到发生将其关闭的事件。
- 以句点、问号、感叹号或冒号结尾的行会关闭续行。(Outlook 假设它是句子的结尾?)
- 关闭续行的行将以换行符开头,但如果它们的长度超过 40 个字符,则会重新启用续行。
- 以制表符开头或结尾的行会关闭延续。
- 以 2 个或更多空格开头的行将关闭继续。
- 以 3 个或更多空格结尾的行将关闭继续。
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.
请注意,我在 Outlook 2007 中尝试了所有这些。YMMV。
因此,如果可能,请以句子终止标点符号、制表符或什至三个空格来结束所有项目符号项。
回答by Mohammad Rasool Javeed
Try this:
尝试这个:
message.setContent(new String(body.getBytes(), "iso-8859-1"),
"text/html; charset=\"iso-8859-1\"");
Regards, Mohammad Rasool Javeed
问候,穆罕默德·拉苏尔·贾维德
回答by Mark
I had the same issue, and found a solution. Try this: %0D%0A
to add a line break.
我遇到了同样的问题,并找到了解决方案。试试这个: %0D%0A
添加换行符。
回答by Mahesh Yadav
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.
在我的应用程序中,我试图发送一封电子邮件,其邮件正文由用户在文本区域中键入。发送邮件时,Outlook 会自动删除用户输入的换行符。
e.g if user entered
Yadav
Mahesh
例如,如果用户输入
Yadav
Mahesh
outlook displayed it as
YadavMahesh
Outlook 将其显示为
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.
解决方案:我将换行符“\r\n”更改为“\par”(记住在RTF代码“\par”末尾打空格)并重新设置换行符。
Cheers,
Mahesh
干杯,
马赫什
回答by stephg
You can force a line break in outlook when attaching one (or two?) tab characters (\t) just before the line break (CRLF).
在换行符 (CRLF) 之前附加一个(或两个?)制表符 (\t) 时,您可以在 Outlook 中强制换行。
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.
它似乎适用于 Outlook 2010。请测试这是否适用于其他版本。
See also Outlook autocleaning my line breaks and screwing up my email format