字符串格式化在发送邮件时不起作用。

huangapple go评论103阅读模式
英文:

String formatting is not working in mail sending

问题

  1. String content =
  2. "Dear user,\n" +
  3. "\n" +
  4. "\t<font face=\"Arial\">Hello</font>\n" +
  5. "\n" +
  6. "Regards,\n" +
  7. "Gen;";
  8. MimeMessage message = emailSender.createMimeMessage();
  9. String encodingOptions = "text/html; charset=UTF-8; format=flowed";
  10. try {
  11. message.setHeader("Content-Type", encodingOptions);
  12. message.setSentDate(new Date());
  13. MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
  14. boolean isHTML = true;
  15. helper.setFrom(mailFrom);
  16. helper.setTo(to.replaceAll("\\s",""));
  17. helper.setSubject(subject);
  18. helper.setText(content, isHTML);
  19. }

接收邮件时,您收到的邮件内容将会是:

  1. Dear user,
  2. Hello
  3. Regards,
  4. Gen
英文:

My string is like below

  1. String content =
  2. &quot;Dear user,
  3. &lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;
  4. Regards,
  5. Gen&quot;;

I am passing this content to the mail sender.

  1. `MimeMessage message = emailSender.createMimeMessage();
  2. String encodingOptions = &quot;text/html; charset=UTF-8; format=flowed&quot;;
  3. try{
  4. message.setHeader(&quot;Content-Type&quot;, encodingOptions);
  5. message.setSentDate(new Date());
  6. MimeMessageHelper helper = new MimeMessageHelper(message, true, &quot;utf-8&quot;);
  7. boolean isHTML = true;
  8. helper.setFrom(mailFrom);
  9. helper.setTo(to.replaceAll(&quot;\\s&quot;,&quot;&quot;));
  10. helper.setSubject(subject);
  11. 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.

  1. Dear user,
  2. Hello
  3. Regards,
  4. Gen

答案1

得分: 3

因为这是 HTML,你需要自己添加换行。我不是前端开发人员,但你可以使用&lt;p&gt;标签或&lt;br/&gt;标签。

尝试类似这样的代码:

  1. String content =
  2. &quot;亲爱的用户&lt;br/&gt;
  3. &lt;p&gt; &lt;font face=&quot;Arial&quot;&gt;你好&lt;/font&gt;&lt;/p&gt;
  4. 致意&lt;br/&gt;
  5. Gen&quot;;
英文:

Since it's HTML you have to add the line breaks yourself. I am not a frontend developer but you can use &lt;p&gt; tag or &lt;br/&gt;.
Try something like:

  1. String content =
  2. &quot;Dear user, &lt;br/&gt;
  3. &lt;p&gt; &lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;&lt;/p&gt;
  4. Regards, &lt;br/&gt;
  5. Gen&quot;;

答案2

得分: 3

以下是翻译好的内容:

这条消息正是您创建的内容。简而言之,HTML 不关心空格和换行符。您可以在线测试您的 HTML,看看它的呈现效果,例如在这里:
https://htmledit.squarefree.com/

为了使您的消息格式正确,可以尝试在以下代码行之间插入一些内容:

  1. &lt;html&gt;
  2. &lt;span&gt;亲爱的用户,&lt;/span&gt;&lt;br&gt;&lt;br&gt;
  3. &lt;font face=&quot;Arial&quot;&gt;你好&lt;/font&gt;&lt;br&gt;&lt;br&gt;
  4. &lt;span&gt;祝好,&lt;/span&gt;&lt;br&gt;
  5. &lt;span&gt;Gen&lt;/span&gt;
  6. &lt;/html&gt;

您需要自行添加换行符。您可能可以省略 &lt;span&gt; 标签。

英文:

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:

  1. &lt;html&gt;
  2. &lt;span&gt;Dear user,&lt;/span&gt;&lt;/br&gt;&lt;/br&gt;
  3. &lt;span&gt;&lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;&lt;span&gt;&lt;/br&gt;&lt;/br&gt;
  4. &lt;span&gt;Regards,&lt;/span&gt;&lt;/br&gt;
  5. &lt;span&gt;Gen&lt;/span&gt;
  6. &lt;/html&gt;

You need to provide breaklines yourself. You could probably omit &lt;span&gt; tags.

huangapple
  • 本文由 发表于 2020年9月4日 20:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定