String 中的转义字符‘\r\n’在用EMAIL发出来后发现在OUTLOOK中没起作用

content.append("Error occurs when allocate this SHTT messages - ").append("\r\n").append("Error Message -").append("\r\n"); 发现在email中的内容中没有换行。

google 了一下,国外的朋友说要在每行最后加个"."就可以了,所以改成

content.append("Error occurs when allocate this SHTT messages.").append("\r\n").append("Error Message -").append("\r\n"); 就没这个问题了。

 

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

link | improve this question

78% accept rate
 
 
feedback

12 Answers

up vote 7 down vote accepted

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

link | improve this answer
 
6 
While this might help simply ending a line in \r\n does not guarantee that Outlook keeps the line break. –  GaussZ Jun 22 '10 at 15:44
feedback

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, you can end messages with a sentence-terminating punctuation mark, or even a tab.

link | improve this answer
 
 
feedback

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 somenewsletters.
  3. You can give up on using a plain text format, and use an html format.
link | improve this answer
 
 
feedback

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\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

link | improve this answer
 
 
feedback

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

link | improve this answer
 
+1 for alternative, I solved with yours only. –  confeng Mar 17 at 11:39
feedback

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

link | improve this answer
 
 
feedback

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

link | improve this answer
 
 
feedback

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

link | improve this answer
 
 
feedback

Try this:

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

Regards, Mohammad Rasool Javeed

link | improve this answer
 
 
feedback

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.

link | improve this answer
 
 
feedback

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

link | improve this answer
 
 
feedback

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) 
转义字符在C++中扮演着重要的角色,它们允许程序员在字符串中插入不能直接键入的特殊字符,或者控制程序的输出格式。例如,使用`\n`来换行或者`\t`来插入制表符。这些字符通过在字符前加反斜杠`\\`来表示,以区分普通字符和转义字符。了解转义字符不仅能够帮助你更好地格式化输出,还能处理文件操作中的特殊字符,比如换行符。 参考资源链接:[C++编程:转义字符完全解析](https://wenku.youkuaiyun.com/doc/77e7pq44zg?spm=1055.2569.3001.10343) 为了深入理解和应用C++中的转义字符,推荐您阅读《C++编程:转义字符完全解析》一书。该书详细讲解了转义字符的语法、用法及其背后的工作原理,对于初学者和经验丰富的开发者都是一个很好的学习资源。 在程序设计中,转义字符不仅用于格式化输出,它们还用于定义多行字符串、处理特殊字符以及在某些情况下用于代码中的注释。例如,单行注释以`//`开始,而多行注释则使用`/*`开始和`*/`结束。此外,转义序列也可以用于字符字面量中,如`'\n'`表示换行字符。正确地使用转义字符能够提高程序的可读性和可维护性。 掌握转义字符的使用对于C++程序员来说是非常基础且必要的。除了字符串格式化之外,转义字符在处理文件I/O操作、正则表达式以及国际化和本地化应用中也具有关键作用。在进行这些高级操作之前,理解转义字符的基础知识至关重要。 此外,C++中的转义序列不仅限于ASCII字符集,还包括对Unicode字符的支持,这在处理国际化文本时尤为有用。例如,使用`\uNNNN`可以表示一个Unicode字符,其中`NNNN`是该字符的Unicode码点。 在学习了转义字符的使用后,为了进一步提升C++编程技能,我建议继续阅读《C++编程:转义字符完全解析》一书的后续章节,它详细介绍了C++的语法结构、位运算、程序调试等其他高级主题,这些都是构建高效和可移植C++程序所必须掌握的知识。 参考资源链接:[C++编程:转义字符完全解析](https://wenku.youkuaiyun.com/doc/77e7pq44zg?spm=1055.2569.3001.10343)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值