java 使用 StringBuilder 格式化电子邮件时换行符不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3104408/
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
NewLine Characters not working when formatting email with StringBuilder
提问by TheJediCowboy
I am doing simple formatting for an email with StringBuilder and have code that looks like the following.
我正在使用 StringBuilder 对电子邮件进行简单的格式化,并且具有如下所示的代码。
StringBuilder message = new StringBuilder();
message.append("Name: " + model.getName() + "\r\n");
message.append("Organization: " + model.getOrganization() +"\r\n");
message.append("Comment: " + model.getComment() +"\r\n");
contactMessage.setMessage(message.toString());
I am logging the formatting and it works correctly, but it is coming out as one line when we actually check the emails being sent.
我正在记录格式并且它可以正常工作,但是当我们实际检查正在发送的电子邮件时,它会作为一行出现。
What if I am not using HTML though is my real question...thanks for the help.
如果我不使用 HTML 怎么办是我真正的问题...感谢您的帮助。
回答by brainimus
What is the format of your email? If the format is HTML newline characters will be ignored and you'd need to insert HTML breaks <br />.
您的电子邮件格式是什么?如果格式是 HTML 换行符将被忽略,你需要插入 HTML 中断<br />。
StringBuilder message = new StringBuilder();
message.append("Name: " + model.getName() + "<br />");
message.append("Organization: " + model.getOrganization() +"<br />");
message.append("Comment: " + model.getComment() +"<br />");
contactMessage.setMessage(message.toString());
回答by jjnguy
If you are formatting HTML emails, then you need to use:
如果您要格式化 HTML 电子邮件,则需要使用:
StringBuilder message = new StringBuilder();
message.append("Name: " + model.getName() + "<br />\n");
message.append("Organization: " + model.getOrganization() +"<br />\n");
message.append("Comment: " + model.getComment() +"<br />\n");
contactMessage.setMessage(message.toString());
You need to insert a html line break because the newlines are ignored.
您需要插入 html 换行符,因为换行符被忽略。

