英文:
String formatting is not working in mail sending
问题
String content = 
    "Dear user,\n" +
    "\n" +
    "\t<font face=\"Arial\">Hello</font>\n" +
    "\n" +
    "Regards,\n" +
    "Gen;";
MimeMessage message = emailSender.createMimeMessage();
String encodingOptions = "text/html; charset=UTF-8; format=flowed";
try {
    message.setHeader("Content-Type", encodingOptions);
    message.setSentDate(new Date());
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
    boolean isHTML = true;
    helper.setFrom(mailFrom);
    helper.setTo(to.replaceAll("\\s",""));
    helper.setSubject(subject);
    helper.setText(content, isHTML);
}
接收邮件时,您收到的邮件内容将会是:
Dear user,
    
    Hello
    
Regards,
Gen
英文:
My string is like below
String content  = 
"Dear user,
	<font face="Arial">Hello</font>
Regards,
Gen";
I am passing this content to the mail sender.
	`MimeMessage message = emailSender.createMimeMessage();
	String encodingOptions = "text/html; charset=UTF-8; format=flowed";
	try{
		message.setHeader("Content-Type", encodingOptions);
		message.setSentDate(new Date());
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
		boolean isHTML = true;
		helper.setFrom(mailFrom);
		helper.setTo(to.replaceAll("\\s",""));
		helper.setSubject(subject);
		helper.setText(content, isHTML);`
And this content String I am passing to the mail send like above, and it is in the format of html. When I recieve the mail I am getting the mail like below
Dear user,Hello Regards,Gen
How can I display the content in the below format.
Dear user,
	Hello
Regards,
Gen
答案1
得分: 3
因为这是 HTML,你需要自己添加换行。我不是前端开发人员,但你可以使用<p>标签或<br/>标签。
尝试类似这样的代码:
String content = 
"亲爱的用户,<br/>
   <p> <font face="Arial">你好</font></p>
致意,<br/>
Gen";
英文:
Since it's HTML you have to add the line breaks yourself. I am not a frontend developer but you can use <p> tag or <br/>.
Try something like:
String content  = 
"Dear user, <br/>
   <p> <font face="Arial">Hello</font></p>
Regards, <br/>
Gen";
答案2
得分: 3
以下是翻译好的内容:
这条消息正是您创建的内容。简而言之,HTML 不关心空格和换行符。您可以在线测试您的 HTML,看看它的呈现效果,例如在这里:
https://htmledit.squarefree.com/
为了使您的消息格式正确,可以尝试在以下代码行之间插入一些内容:
<html>
<span>亲爱的用户,</span><br><br>
<font face="Arial">你好</font><br><br>
<span>祝好,</span><br>
<span>Gen</span>
</html>
您需要自行添加换行符。您可能可以省略 <span> 标签。
英文:
The message is exactly what you've created. In short HTML doesn't care about spaces and newlines. You can test your HTML online to see how it looks, ie. here:
https://htmledit.squarefree.com/
For your message to be properly formatted, try something between the lines of:
<html>
<span>Dear user,</span></br></br>
<span><font face="Arial">Hello</font><span></br></br>
<span>Regards,</span></br>
<span>Gen</span>
</html>
You need to provide breaklines yourself. You could probably omit <span> tags.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论